源码地址: https://github.com/JianBiHua/go_line_chat
网上有一个TCP/UDP测试工具叫SocketTool,支持MAC/Window, 自己百度下载下。
我们先下载上面的代码,如果直接运行,本机测试,则IP为127.0.0.1,端口为8888;用socketTool创建一个Tcp Client 如下图为连接状态,说明服务端已启动:
然后我们模拟发送我们的消息,可以看到我们的发送的消息,以及接收到服务器的反馈消息(不要在意为啥两张图的端口不一样, 为了截图,我开了两次, 嘿嘿):
消息如下:
// 注册
{
"type":"SignIn",
"user":"test1",
"psw":"pasw"
}
// 登陆
{
"type":"LoginIn",
"user":"test1",
"psw":"pasw"
}
// 登出
{
"type":"LoginOut"
}
// 获取好友列表
{
"type" : "FriendGet"
}
// 添加好友
{
"type" : "Add",
"user" : "test2"
}
// 删除好友
{
"type" : "Del",
"user" : "test1"
}
// 获取聊天组
{
"type" : "GroupGet"
}
// 创建组
{
"type" : "GroupCreate",
"users" :["test2"],
"groupType" : 0
}
// 发送消息
{
"type" : "Chat",
"group" : 2,
"msg" : "我发来一条消息",
"msgType" : 0
}
// 获取组2的消息
{
"type" : "Msg",
"group" : 2
}