文:徐江威
时信魔方的即时消息通过 MessagingService 模块来操作。在 Cube 启动之后使用 MessagingService 的 sendTo
、sendToContact
或者 sendToGroup
向指定的联系人或群组发送消息。通过加入事件监听器来接收 MessagingService 的事件,从而实现接收来自其他联系人或者群组的消息。
启动 Cube Engine
一般实例化 Cube Engine 之后,通过配置相关的域参数即可启动 Cube 。
Cube 支持多域管理,因此每个客户端设备启动 Cube 时都需要标记自己所在的域信息,这些信息包括 Cube 的网关机地址、域信息和 App Key 等。例如:
{
"address": "127.0.0.1",
"domain": "shixincube.com",
"appKey": "shixin-cubeteam-opensource-appkey"
}
这个 JSON 表示的配置信息就包括服务器的地址 address
,所在域的名称 domain
,以及这个域里的 appKey
识别串。
各个客户端的启动代码如下:
Web 版
const cube = window.cube();
const config = {
"address": "127.0.0.1", "domain": ... };
cube.start(config, function() {
console.log('Cube 启动成功');
}, function() {
console.log('Cube 启动错误');
});
Android 版
KernelConfig config = new KernelConfig();
config.address = "127.0.0.1";
config.domain = "shixincube.com";
config.appKey = "shixin-cubeteam-opensource-appkey";
CubeEngine.getInstance().start(context, config);
iO