android简单的聊天记录,Tio

编程入门 行业动态 更新时间:2024-10-26 12:25:18

android简单的<a href=https://www.elefans.com/category/jswz/34/1762589.html style=聊天记录,Tio"/>

android简单的聊天记录,Tio

一、TioClient初始化

1.获取IM服务器地址// 创建Tio客户端

TioClient mClient = new TioClient();

ImServerAddress address = mClient.requestAddress(null);

String ip = address.getIp();

int port = address.getPort();

2.初始化TioClient配置ip: IM服务器ip

port: IM服务器port

groupId: 群组id(可以为空)

timeout: 连接超时时长(单位毫秒;默认15000毫秒)

heartBeatInterval: 心跳间隔(单位秒;默认60秒;至少1秒)

reconnectInterval: 重连间隔(单位秒;默认3秒;至少1秒) mClient.initConfig(new TaoIMConfig.Builder(ip, port)

.setTimeout(timeout)

.setHeartBeatInterval(heartBeatInterval)

.setReconnectInterval(reconnectInterval)

.build());

3.设置握手请求token: 客户端通过http登录后,服务器返回给客户端的token值

handshakeKey: 握手请求密钥,存在:org.tio.sitexxx.service.vo.Const.HANDSHAKE_KEY中,可以修改,但要保证客户端和服务器端用的这个值是一样的

cid: 渠道 // 内部会自动创建握手请求消息包,并发送

mClient.setTioHandshake(new TioHandshake.Builder(token, handshakeKey)

.setActivity(activity)

.setCid(cid)

.build());

4.设置“命令码所对应的消息体”Map// Short为消息命令码,Class为消息体字节码

Map commandBodyMap = new HashMap<>();

commandBodyMap.put(TioCommand.HANDSHAKE_RESP.getValue(), HandshakeResp.class);

commandBodyMap.put(TioCommand.JOIN_GROUP_RESP.getValue(),JoinGroupResp.class);

...

mClient.setCommandBodyMap(commandBodyMap);

5.注册回调,接收消息//注册监听

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 加入群组响应

}

}

});

二、TioClient基本操作

开始连接 mClient.connect();

断开连接 mClient.disconnect();

获取连接状态 TaoIMState imState = mClient.getState()

发消息 client.sendPacket(packet)

三、TioClient资源释放@Override

protected void onDestroy() {

super.onDestroy();

if (mClient != null) {

mClient.release();

mClient = null;

}

}

更多推荐

android简单的聊天记录,Tio

本文发布于:2024-02-12 22:48:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1689706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:聊天记录   简单   android   Tio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!