1

JAVA Socket编程:编写即时通讯系统

 3 years ago
source link: https://arminli.com/java-socket-im/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Armin's Blog

JAVA Socket编程:编写即时通讯系统

March 28, 2016

  • 发送文件、表情、信息
  • 多人同时聊天
  • 离线发送消息(上线后就能看到)

服务器和客户端通信协议

客户端登录,客户端取得用户列表,以及客户端和客户端的通信都必须跟服务器交互,我们约定客户端和服务器端的通信协议如下:

请求数据格式

客户端发送请求数据格式:

请求码 请求数据

请求码:
101 用户登录
102 取得用户列表
103 发送消息

如登录请求,客户端向服务器发送的请求数据为:
101|用户名,密码

如取得用户列表,客户端向服务器发送的请求数据为:
102|

如发送消息,客户端向服务器发送的请求数据为:
103|消息发送方,消息接收方,消息内容

响应数据格式

服务器响应数据格式:
响应码 响应数据

响应码:
1 响应成功
0 响应失败

如登录请求,服务器端的响应数据为:
1| 或者 0|

如取得用户列表,服务器端的响应数据为:
1|消息类型(102)@消息发送方(Server)@用户1,用户2,用户3@消息发送时间

如服务器给客户端发送消息
1|消息类型(103)@消息发送方@消息内容@消息发送时间

服务器消息队列格式

服务器端接收到客户的请求后(取得用户列表和用户信息)将请求结果放在消息队列中,发送消息的线程用队列中取消息,发送给客户端,

我们约定消息队列中消息的格式如下:

消息发送方 消息接收方 内容 发送时间 消息类别 Server tom Tom,jack,luly,lily 2016-1-29 11:11:11 102 Jack tom How are you doing? 2016-1-29 12:12:12 103

Profile picture

Written by Armin Li , a venture capitalist. [Weibo] [Subscribe]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK