配图:蜈支洲岛
本文档针对于23456端口的通讯协议说明
消息传输使用了Gogle Protobuf编码解码.所以Ios , actionscript, c++,c#,等语言的客户端可以使用 socket 连接服务端,发送规定的数据消息即可。
1、消息结构
消息结构采用TLV格式(Tag,Length,Value)
| Tag | Length | Value |
|---|---|---|
| 1个字节 | 2个字节 | Length字节消息体 |
1.1消息类型规定
| 值 | 长度 | 发送方 | 说明 |
|---|---|---|---|
| 0 | 1个字节 | 客户端 | PONG 心跳响应 |
| 1 | 1个字节 | 服务端 | PING 心跳请求 |
| 2 | 1个字节 | 服务端 | 推送消息Message |
| 3 | 1个字节 | 客户端 | 业务请求SentBody |
| 4 | 1个字节 | 服务端 | 业务处理响应ReplyBody |
1.2 消息体组装
无论是服务端写入或者客户端写入任何消息,其中消息头在一个消息的最前面占3位,第一位消息类型,第二和第三位消息的byte[]长度
消息长度 int 转换 byte[]算法 ,最大支持消息长度为65535
length转换2位byte数组
写入时构建请求头
private byte[] createLengthHeader(byte type,int length){byte[] header = newbyte[3];header[0] = type;header[1] = (byte) (length & 0xff);header[2] = (byte) ((length >> 8) & 0xff);return header;}
读取消息头 获取消息长度
int lv = header[1];int hv = header[2];private int getContentLength(byte lv,byte hv){int l = (lv & 0xff);int h = (hv & 0xff);return (l| h << 8);}
2 发送绑定账号请求
| 字段名称 | 类型 | 说明 |
|---|---|---|
| uid | String | 用户账号 |
| deviceId | String | 设备ID,ios则为deviceToken |
| channel | String | 客户端类型,取值为 android , ios , windows ,mac 都是小写 |
| deviceName | String | 设备型号名称 |
| appVersion | String | 客户端应用版本号 |
| osVersion | String | 系统版本号 |
| packageName | String | 包名 |
| language | String | 客户端语言,如zh-CN |
使用SentBody.proto生产成对应语言的实体
Key 为 client_bind
请求示例
byte SENT_BODY = 3;byte[] body = sentBody.data;byte[] data= new Byte[body.length+3];byte[] header = createLengthHeader(SENT_BODY,body.length);data[0] = header[0];data[1] = header[1];data[2] = header[2];System.arraycopy(body,0,data,3,length)socket.write(data);
3 客户端收到数据
服务端会发送3种数据到客户端 ,参见1.2
获取收到的数据的第一个字节也是就是byte type = data[0]
| 值 | 说明 | 处理 |
|---|---|---|
| 1 | 收到了 PING | 立即响应PONG 参见4 |
| 2 | 收到了Message | 解析做业务处理 |
| 4 | 收到了ReplyBody | 解析做业务处理 |
byte type = headerBuffer.get();byte lv = headerBuffer.get();byte hv = headerBuffer.get();int dataLength = getContentLength(lv, hv);ByteBuffer bodyBuffer = ByteBuffer.allocate(dataLength);do {socketChannel.read(bodyBuffer);} while (bodyBuffer.position() != dataLength);byte[] data = bodyBuffer.array();
MESSAGE
使用Message.proto生产成对应语言的实体
取出消息头中的消息长度,通过消息长度再取出消息数据
将消息内容通过protobuf转换为Message.proto生成的结构
byte[] data = bodyBuffer.array();MessageProto.Model bodyProto = MessageProto.Model.parseFrom(bodyBuffer.array());
REPLY_BODY
使用Reply.proto生产成对应语言的实体
取出消息头中的消息长度,通过消息长度再取出消息数据
将消息内容通过protobuf转换为Reply.proto生成的结构
byte[] data = bodyBuffer.array();ReplyBodyProto.Model bodyProto = ReplyBodyProto.Model.parseFrom(bodyBuffer.array());
3.1消息断包\粘包处理
通过解析消息头获得消息体长度
读取消息体不够长度则说明发送了断包,需要继续等待后续消息,直到满足while条件 说明收到了一条完整的消息
断包:读取的数据中不是一条完整的消息
粘包:读取的数据中有一条完整的消息和另一条消息的部分数据
byte type = headerBuffer.get();byte lv = headerBuffer.get();byte hv = headerBuffer.get();/** 先通过消息头拿到消息的长度,然后进行定长读取* 解决消息的断包和粘包情况*/int dataLength = getContentLength(lv, hv);ByteBuffer bodyBuffer = ByteBuffer.allocate(dataLength);/** 如果读取的消息长度不够,则进行等待后续消息到来* 当读取的消息长度(bodyBuffer.position() == dataLength)时意味着一个完整的消息已经接收完成*/do {socketChannel.read(bodyBuffer);} while (bodyBuffer.position() != dataLength);byte[] data = bodyBuffer.array();
4 客户端心跳响应
心跳由服务的主动向客户端发起,写空闲隔为45秒一次,用于检查客户端是否还在线,服务的发起心跳请求命令为PING当客户端收到心跳请求应立即回应PONG给服务器,否则服务端将会在10秒后断开连接
发送心跳响应
byte PONG = 0;byte[] body = "PONG".getBytes();byte[] data= new Byte[body.length+3];byte[] header = createLengthHeader(PONG,body.length);data[0] = header[0];data[1] = header[1];data[2] = header[2];System.arraycopy(body,0,data,3,length)socket.write(data);

