SOCKET
链接支持IPV6
接口- 兼容
DNS64/NAT64
网络环境
- 在
Unity
主线程中初始化RTMRegistration.Register()
- 若
RTMRegistration
已初始化,RTMClient
可在任意线程中构造和使用(线程安全) - 异步函数均由子线程呼叫,不要在其中使用仅UI线程的函数,不要阻塞异步函数
- 默认连接会自动保持,如实现按需连接则要通过
Login()
和Close()
进行连接或关闭处理 - 或可通过
login
和close
事件以及注册ping
服务来对连接进行管理 - 消息发送接口仅支持
UTF-8
格式编码的string
类型数据,Binary
数据需进行Base64
编解码
event
:login
: 登陆exception
: (Exception) auth失败, token失效需重新获取payload
: (IDictionary) 当前连接的RTMGate地址, 可在本地缓存, 下次登陆可使用该地址以加速登陆过程, 每次登陆成功需更新本地缓存
error
: 发生异常exception
: (Exception)
close
: 连接关闭retry
: (bool) 是否执行自动重连(未启用或者被踢掉则不会执行自动重连)
using System;
using System.Collections;
using System.Collections.Generic;
using GameDevWare.Serialization;
using com.rtm;
using UnityEngine;
...
//UnityMainThread
RTMRegistration.Register();
//AnyThread
RTMClient client = new RTMClient(
"52.83.245.22:13325",
1000012,
654321,
"3993142515BD88A7156629A3AE550B9B",
RTMConfig.TRANS_LANGUAGE.en,
new Dictionary<string, string>(),
true,
20 * 1000,
true
);
//添加事件监听
client.GetEvent().AddListener("login", (evd) => {
if (evd.GetException() != null) {
Debug.Log("Auth Fail!");
return;
}
Debug.Log("Authed! RTM Gate Endpoint: " + evd.GetPayload());
//发送业务消息(发送聊天消息请使用SendChat)
client.SendMessage(778899, (byte) 8, "hello !", "", 0, 5 * 1000, (cbd) => {
object obj = cbd.GetPayload();
if (obj != null) {
Debug.Log("[DATA] SendMessage: " + Json.SerializeToString(obj) + ", mid: " + cbd.GetMid());
} else {
Debug.Log("[ERR] SendMessage: " + cbd.GetException().Message);
}
});
});
client.GetEvent().AddListener("close", (evd) => {
Debug.Log("Closed! Retry: " + evd.HasRetry());
});
client.GetEvent().AddListener("error", (evd) => {
Debug.Log("Error: " + evd.GetException().Message);
});
//添加推送监听
RTMProcessor processor = client.GetProcessor();
processor.AddPushService(RTMConfig.SERVER_PUSH.recvMessage, (data) => {
Debug.Log("[PUSH] Recv Msg: " + Json.SerializeToString(data));
});
//开启连接
client.Login(null);
//Destroy
// client.Destroy();
// client = null;