1.10.2.3.3 长连接通道 SDK
优质
小牛编辑
134浏览
2023-12-01
长连接通道 SDK
更新时间:2018-03-26 19:22:29
概要
长连接通道SDK,提供 IoT 业务协议封装的云端数据下行能力;为 app 提供订阅、发布消息的能力, 和支持请求响应模型。
依赖SDK | 概述 |
---|---|
API 通道 | 提供API通道能力 |
初始化
初始化 Mobile 长连接通道 SDK 前,请先初始化 API 通道 SDK,请参见 API 通道 - 初始化。Mobile 长连接通道 SDK 的初始化配置可以参照 MobileConnectConfig , 示例初始化代码如下:
//打开Log 输出
ALog.setLevel(ALog.LEVEL_DEBUG);
MobileConnectConfig config = new MobileConnectConfig();
// 设置 appKey 和 authCode
config.appkey = "{YOUR_APP_KEY}";
config.securityGuardAuthcode = "114d";
// 设置验证服务器
config.authServer = "api.link.aliyun.com";
MobileChannel.getInstance().startConnect(context, config, new IMobileConnectListener() {
@Override
public void onConnectStateChange(MobileConnectState state) {
ALog.d(TAG,"onConnectStateChange(), state = "+state.toString());
}
});
使用方式
SDK 封装了上行 RPC 请求、订阅、取消订阅等接口,详细接口可以参考 IMobileChannel
业务请求响应模型
业务请求响应模型示例:
//带请求响应的 RPC 请求
String topic = "path/of/topic";
JSONObject params = new JSONObject();
params.put("key","value");
MobileChannel.getInstance().asyncSendRequest(topic, null, params, new IMobileRequestListener() {
@Override
public void onSuccess(String jsonData) {
ALog.d(TAG,"onSuccess, rsp = "+jsonData);
}
@Override
public void onFailure(AError error) {
ALog.d(TAG,"onFailure");
}
});
订阅 Topic
//订阅请求
String topic = "path/of/topic";
MobileChannel.getInstance().subscrbie(topic, new IMobileSubscrbieListener() {
@Override
public void onSuccess(String topic) {
ALog.d(TAG,"onSuccess, topic = "+topic);
}
@Override
public void onFailed(String topic, AError error) {
ALog.d(TAG,"onFailed, topic = "+topic);
}
@Override
public boolean needUISafety() {
return false;
}
});
取消订阅 Topic
//取消订阅
String topic = "path/of/topic";
MobileChannel.getInstance().unSubscrbie(topic, new IMobileSubscrbieListener() {
@Override
public void onSuccess(String topic) {
ALog.d(TAG,"onSuccess, topic = "+topic);
}
@Override
public void onFailed(String topic, AError error) {
ALog.d(TAG,"onFailed, topic = "+topic);
}
@Override
public boolean needUISafety() {
return false;
}
});
Publish 数据
//Publish 请求
String topic = "path/of/topic";
JSONObject params = new JSONObject();
params.put("key","value");
MobileChannel.getInstance().ayncSendPublishRequest(topic, params, new IMobileRequestListener() {
@Override
public void onSuccess(String jsonData) {
ALog.d(TAG,"onSuccess, rsp = "+jsonData);
}
@Override
public void onFailure(AError error) {
ALog.d(TAG,"onFailure");
}
});
注册下行 Listener
设置长连接通道连接变化以及云端推送的下行消息监听事件。详细接口可以参考 IMobileChannel
/**
* 设置通道的下推回调,如果不需要用的时候,记得调用 unRegisterDownstreamListener
*/
MobileChannel.getInstance().registerDownstreamListener(true, new IMobileDownstreamListener() {
@Override
public void onCommand(String method, String data) {
ALog.d(TAG,"接收到Topic = "+method+", data="+data);
}
@Override
public boolean shouldHandle(String method) {
return true;
}
});
/** 注册通道的状态变化,记得调用 unRegisterConnectListener
*/
MobileChannel.getInstance().registerConnectListener(true, new IMobileConnectListener() {
@Override
public void onConnectStateChange(MobileConnectState state) {
ALog.d(TAG,"通道状态变化,state="+state);
}
});
混淆配置
-keep class com.aliyun.alink.linksdk.channel.**{*;}