长连接通道SDK
长连接通道SDK,提供IoT业务协议封装的云端数据下行能力,为App提供订阅、发布消息的能力和支持请求响应模型。
初始化
使用方式
SDK封装了上行RPC请求、订阅、取消订阅等接口 。
SDK中描述的Topic都是简短的Topic。例如:
完整的上行请求Topic:
/sys/${productKey}/${deviceName}/app/up/test/publish,上行请求SDK内部会判断补齐/sys/${productKey}/${deviceName}/app/up/,所以在调用SDK入参的时候只需要输入/test/publish。对应的下行Topic。例如:
完整的设备状态变化下行Topic:
/sys/${productKey}/${deviceName}/app/down/things/status,SDK回调里面只会出现/things/status,自动阶段调用/sys/${productKey}/${deviceName}/app/down前缀。
业务请求响应模型
该接口实际上是封装了一个Remote Procedure Call的过程。以用户账号绑定通道的示例来说明内部逻辑,如下图所示。
例如:用户账号绑定通道的Topic为/sys/${productKey}/${deviceName}/app/up/account/bind。
向该Topic发布数据前,需先订阅该Topic对应的Reply Topic:
/sys/${productKey}/${deviceName}/app/down/account/bind_reply。订阅成功后,SDK通过Topic:
/sys/${productKey}/${deviceName}/app/up/account/bind向设备所在IoT服务发送账号绑定数据,IoT服务用户完成账号绑定的业务逻辑后,向Reply Topic:/sys/${productKey}/${deviceName}/app/down/account/bind_reply发布响应数据。SDK在收到Reply Topic的数据后,将响应结果通过
onSuccess回调给客户端App,从而完成整个业务逻辑。
业务请求响应模型示例如下:
#import <IotConnectChannel/IotConnectChannel.h>
//由于长连接通道 SDK,会在内部逻辑中补齐 '/sys/{productKey}/{deviceName}/app/up' 部分,
//所以使用者在这个 API 时,只要传这一段的后边部分即 '/account/bind' 即可了。
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
@"value", @"key" ,nil];
[[IotConnectChannel sharedInstance] invokeWithTopic: @"path/of/topic" params:dic
respHandler:^(IotConnectChannelResponse * _Nullable response) {
if(![response successed]){
NSLog(@"[ConnectChannel] invokeWithTopic error:%@",[response responseError].description);
}else{
NSLog(@"[ConnectChannel] invokeWithTopic success");
}
}];订阅Topic
单个订阅Topic
#import <IotConnectChannel/IotConnectChannel.h> NSString *topic1 = @"path/of/topic1"; [[IotConnectChannel sharedInstance] subscribe:topic1 complete:^(NSError * _Nullable error) { if(error){ NSLog(@"[ConnectChannel] subscribe error:%@",error.description); }else{ NSLog(@"[ConnectChannel] subscribe success"); } }];批量订阅Topic
#import <IotConnectChannel/IotConnectChannel.h> NSString *topic1 = @"path/of/topic1"; NSString *topic2 = @"path/of/topic2"; NSString *topic3 = @"path/of/topic3"; NSMutableArray *topicArray = [NSMutableArray array]; [topicArray addObject:topic1]; [topicArray addObject:topic2]; [topicArray addObject:topic3]; [[IotConnectChannel sharedInstance] subscribeMany:topicArray complete:^(NSError * _Nullable error) { if(error){ NSLog(@"[ConnectChannel] subscribeMany error:%@",error.description); }else{ NSLog(@"[ConnectChannel] subscribeMany success"); } }];
取消订阅Topic
单个取消订阅Topic
#import <IotConnectChannel/IotConnectChannel.h> NSString *topic1 = @"path/of/topic1"; [[IotConnectChannel sharedInstance] unsubscribe:topic1 complete:^(NSError * _Nullable error) { if(error){ NSLog(@"[ConnectChannel] unsubscribe error:%@",error.description); }else{ NSLog(@"[ConnectChannel] unsubscribe success"); } }];批量取消订阅Topic
#import <IotConnectChannel/IotConnectChannel.h> NSString *topic1 = @"path/of/topic1"; NSString *topic2 = @"path/of/topic2"; NSString *topic3 = @"path/of/topic3"; NSMutableArray *topicArray = [NSMutableArray array]; [topicArray addObject:topic1]; [topicArray addObject:topic2]; [topicArray addObject:topic3]; [[IotConnectChannel sharedInstance] unsubscribeMany:topicArray complete:^(NSError * _Nullable error) { if(error){ NSLog(@"[ConnectChannel] unsubscribeMany error:%@",error.description); }else{ NSLog(@"[ConnectChannel] unsubscribeMany success"); } }];
Publish数据
上行请求推荐使用API通道。
#import <IotConnectChannel/IotConnectChannel.h>
NSString *topic1 = @"path/of/topic1";
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
@"on", @"Switch",
@(FALSE), @"Button",
@42,@"Number",nil];
[[IotConnectChannel sharedInstance] publish:topic1 params:dic complete:^(NSError * _Nullable error) {
if(error){
NSLog(@"[ConnectChannel] publish error:%@",error.description);
}else{
NSLog(@"[ConnectChannel] publish success");
}
}];注册和注销下行监听器(Listener)
设置长连接通道连接变化以及云端推送的下行消息监听事件。
目前云端下行接口包括设备属性、事件及连接状态变更推送,相关Topic说明,请参见长连接服务API。这些Topic在SDK启动时会自动订阅。
状态监听器
#import <IotConnectChannel/IotConnectChannel.h>
interface ChannelConnectionListenerImpl : NSObject<IotConnectChannelConnectListener>
@end
@implementation ChannelConnectionListenerImpl
- (void) onConnectState:(IotConnectChannelConnectState)state{
NSLog(@"[ConnectChannel] onConnectionState id = %@ state = %ld",_implId,state);
}
@end
@property ChannelConnectionListenerImpl *statelistener;
-(void)register{ //注册状态监听
_statelistener = [ChannelConnectionListenerImpl new];
[[IotConnectChannel sharedInstance] addConnectListener:_statelistener];
}
-(void)unRegister{//注销状态监听
[[IotConnectChannel sharedInstance] removeConnectListener:_statelistener];
}下行流监听器
#import <IotConnectChannel/IotConnectChannel.h>
@interface ChannelDownstreamListenerImpl : NSObject<IotConnectChannelDownstreamListener>
@end
@implementation ChannelDownstreamListenerImpl
- (void)onDownstream:(NSString * _Nonnull)topic data:(id _Nullable)data{
NSLog(@"[ConnectChannel] Downstream onDownstream id = %@ topic = %@ data = %@",_implId,topic,data);
}
- (BOOL)shouldHandle:(NSString * _Nonnull)topic{
NSLog(@"[ConnectChannel] Downstream shouldHandle id = %@ topic = %@",_implId,topic);
return true;
}
@end
@property ChannelDownstreamListenerImpl *downlistener;
-(void)register{ //注册下行流监听器
_downlistener = [ChannelDownstreamListenerImpl new];
[[IotConnectChannel sharedInstance] addDownStreamListener:_downlistener];
}
-(void)unRegister{//注销下行流监听器
[[IotConnectChannel sharedInstance] removeDownStreamListener:_downlistener];
}解绑长连接通道与账号
在初始化时,已实现长连接通道与账号的绑定,此时如果您需要解绑长连接通道与账号,参照以下代码执行。
如需解绑长连接通道跟用户账号的绑定,请在账号登出前操作,否则会导致解绑失败。
#import <IotConnectChannel/IotConnectChannel.h>
[[IotConnectChannel sharedInstance] unBindAccount:^(NSError * _Nullable error) {
if(error){
NSLog(@"[ConnectChannel] unBindAccount error:%@",error.description);
}else{
NSLog(@"[ConnectChannel] unBindAccount success");
}
}];