名字
这里以群聊为例,首先看下效果图:
19:56:54 发送一条群聊请求
19:56:54 收到一条群聊通知
public enum MyCommand {
/**
* 群聊请求
*/
GROUP_CHAT_REQ((short) 10),
public static MyCommand from(Short value) {
MyCommand[] values = MyCommand.values();
for (MyCommand v : values) {
if (v.value.equals(value)) {
return v;
}
}
return null;
}
Short value;
private MyCommand(Short value) {
this.value = value;
}
public Short getValue() {
return value;
}
public void setValue(Short value) {
this.value = value;
}
}
群聊请求消息体结构:
public class GroupChatReq {
public GroupChatReq(String c, Integer[] at, String g) {
this.c = c;
this.at = at;
this.g = g;
}
/**
* 聊天内容
*/
private String c;
/**
* 艾特哪些用户。此值可为null。
* 举例:[434343, 9898989]
*/
private Integer[] at;
/**
* groupid
* 举例:45454
*/
private String g;
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public Integer[] getAt() {
return at;
}
public void setAt(Integer[] at) {
this.at = at;
}
public String getG() {
return g;
}
public void setG(String g) {
this.g = g;
}
}
public class MyPacketHolder {
/**
* 组装一个群聊请求消息包
*
* @param content 内容
* @param at 艾特哪些用户。此值可为null。举例:[434343, 9898989]
* @param groupId 群组id
* @return 群聊请求消息包
*/
public static TioPacket getGroupChatReq(String content, Integer[] at, String groupId) {
GroupChatReq req = new GroupChatReq(content, at, groupId);
return getPacket(req, MyCommand.GROUP_CHAT_REQ);
}
public static TioPacket getPacket(Object body, MyCommand command) {
byte[] bodyBytes = null;
if (body != null) {
bodyBytes = ByteUtils.stringToBytes(JsonUtils.object2String(body), TioPacket.CHARSET);
}
short bodyLength = 0;
if (bodyBytes != null) {
bodyLength = (short) bodyBytes.length;
}
TioPacket packet = new TioPacket();
packet.setBodyLength(bodyLength);
packet.setCommand(command.getValue());
packet.setGzip(TioPacket.GZIP);
if (bodyBytes != null) {
packet.setBody(bodyBytes);
}
return packet;
}
}
//组装群聊消息包
TioPacket groupChatReq = MyPacketHolder.getGroupChatReq(
"hi~ tio-android",//发送的内容
null,//艾特哪些用户
EditTextUtils.getString(et_groupId)//群组id
);
//发送消息
mClient.sendPacket(groupChatReq);
最新评论 我的评论
t-io为本站提供HTTP、WebSocket、Socket、页面渲染与压缩等服务,nginx为本站提供反向代理服务
© 2017-2023 钛特云 版权所有 | 浙ICP备17032976号 | 浙公网安备 33011802002129号