名字
// 创建Tio客户端
TioClient mClient = new TioClient();
ImServerAddress address = mClient.requestAddress(null);
String ip = address.getIp();
int port = address.getPort();
mClient.initConfig(new TaoIMConfig.Builder(ip, port)
.setTimeout(timeout)
.setHeartBeatInterval(heartBeatInterval)
.setReconnectInterval(reconnectInterval)
.build());
// 内部会自动创建握手请求消息包,并发送
mClient.setTioHandshake(new TioHandshake.Builder(token, handshakeKey)
.setActivity(activity)
.setCid(cid)
.build());
// Short为消息命令码,Class为消息体字节码
Map<Short, Class> commandBodyMap = new HashMap<>();
commandBodyMap.put(TioCommand.HANDSHAKE_RESP.getValue(), HandshakeResp.class);
commandBodyMap.put(TioCommand.JOIN_GROUP_RESP.getValue(),JoinGroupResp.class);
...
mClient.setCommandBodyMap(commandBodyMap);
//注册监听
mClient.registerTioCallback(new TioSimpleClientCallback() {
// IM连接成功
@Override
public void onConnected(TioClient client) {
}
// IM断开连接
@Override
public void onDisconnected(TioClient client) {
}
// IM连接出错
@Override
public void onError(TioClient client, Exception e) {
}
// IM发送一个消息包完成
@Override
public void onSendEnd(TioClient client, TioPacket packet) {
}
// IM接收一个消息包完成
@Override
public void onReceiveEnd(TioClient client, TioPacket packet, Object body) {
if (packet.getCommand() == TioCommand.HANDSHAKE_RESP.getValue()) {
HandshakeResp handshakeResp = (HandshakeResp) body;
// TODO 握手响应
} else if (packet.getCommand() == TioCommand.JOIN_GROUP_RESP.getValue()) {
JoinGroupResp joinGroupResp = (JoinGroupResp) body;
// TODO 加入群组响应
}
}
});
开始连接
mClient.connect();
断开连接
mClient.disconnect();
获取连接状态
TaoIMState imState = mClient.getState()
发消息
client.sendPacket(packet)
@Override
protected void onDestroy() {
super.onDestroy();
if (mClient != null) {
mClient.release();
mClient = null;
}
}
最新评论 我的评论
t-io为本站提供HTTP、WebSocket、Socket、页面渲染与压缩等服务,nginx为本站提供反向代理服务
© 2017-2023 钛特云 版权所有 | 浙ICP备17032976号 | 浙公网安备 33011802002129号