Skip to content

zsl883300/rtm-client-sdk-unity

 
 

Repository files navigation

fpnn rtm sdk unity

依赖

IPV6

  • SOCKET链接支持IPV6接口
  • 兼容DNS64/NAT64网络环境

其他

  • Unity主线程中初始化RTMRegistration.Register()
  • RTMRegistration已初始化,RTMClient可在任意线程中构造和使用(线程安全)
  • 异步函数均由子线程呼叫,不要在其中使用仅UI线程的函数,不要阻塞异步函数
  • 默认连接会自动保持,如实现按需连接则要通过Login()Close()进行连接或关闭处理
  • 或可通过loginclose事件以及注册ping服务来对连接进行管理
  • 消息发送接口仅支持UTF-8格式编码的string类型数据,Binary数据需进行Base64编解码

Events

  • 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;

接口说明

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%