长连接通道 SDK
更新时间:2018-05-25 13:51:36
概述
长连接通道SDK,提供 IoT 业务协议封装的云端数据下行能力;为 app 提供订阅、发布消息的能力, 和支持请求响应模型。
| 依赖SDK | 概述 |
|---|---|
| 日志 | 基础依赖SDK,提供客户端统一日志打印,日志等级控制,分模块日志隔离等能力 |
| API 通道 | 提供API通道能力,和基础环境配置信息 |
集成方式
如何集成 SDK,查看这里>
初始化
长连接通道 SDK 的初始化依赖 API 通道的初始化,请参见:API 通道 - 初始化
在 API 通道初始化成功后,再参考下面的示例代码初始化长连接通道 SDK:
#import <IMSApiClient/IMSApiClient.h>
#import <AlinkAppExpress/LKAppExpress.h>
IMSConfiguration * imsconfig = [IMSConfiguration sharedInstance];
LKAEConnectConfig * config = [LKAEConnectConfig new];
config.appKey = imsconfig.appKey;
config.authCode = imsconfig.authCode;
// 指定长连接服务器地址。 (默认不填,SDK会使用默认的地址及端口。默认为国内华东节点。不要带 "协议://",如果置为空,底层通道会使用默认的地址)
config.server = @""
// 开启动态选择Host功能。 (默认false,海外环境建议设置为true。此功能前提为ChannelHost 不特殊指定。)
config.autoSelectChannelHost = false;
[[LKAppExpress sharedInstance]startConnect:config connectListener:self];
使用说明
业务请求响应模型
#import <AlinkAppExpress/LKAppExpress.h>
[[LKAppExpress sharedInstance] invokeWithTopic : @"/account/bind" opts:nil params:@{@"iotToken":iotToken}
respHandler:^(LKAppExpResponse * _Nonnull response) {
LKAELogDebug(@"bindAccount result : %@", response);
}];
订阅 Topic
#import <AlinkAppExpress/LKAppExpress.h>
//已订阅用户所绑定的设备属性变化事件为例,详细的使用说明请参考 api reference
[[LKAppExpress sharedInstance]subscribe:@"/properties/post" complete:^(NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error == nil) {
[_tipsLabel setText:@"订阅成功"];
} else {
[_tipsLabel setText:@"订阅失败"];
}
});
}];
取消订阅 Topic
#import <AlinkAppExpress/LKAppExpress.h>
// 详细的使用说明请参考 api reference
[[LKAppExpress sharedInstance]unsubscrbie:@"/properties/post" complete:^(NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error == nil) {
[_tipsLabel setText:@"取消订阅成功"];
} else {
[_tipsLabel setText:@"取消订阅失败"];
}
});
}];
Publish 数据
#import <AlinkAppExpress/LKAppExpress.h>
NSString * text = @"{\"input\":\"Hello World\"}";
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
if (data == nil) {
return;
}
NSError *error;
NSDictionary *params = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) {
return;
}
[[LKAppExpress sharedInstance]publish:@"/test/publish" params:params
complete:^(NSError * _Nonnull error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error == nil) {
[_tipsLabel setText:@"publish成功"];
} else {
[_tipsLabel setText:@"publish失败"];
}
});
}];
注册下行 Listener
#import <AlinkAppExpress/LKAppExpress.h>
@interface TestDownstreamListener : NSObject <LKAppExpDownListener>
@end
@implementation TestDownstreamListener
- (void)onDownstream:(NSString * _Nonnull)topic data:(id _Nullable)data {
NSLog(@"onDownstream topic : %@", topic);
NSLog(@"onDownstream data : %@", data);
NSDictionary * replyDict = nil;
if ([data isKindOfClass:[NSString class]]) {
NSData * replyData = [data dataUsingEncoding:NSUTF8StringEncoding];
replyDict = [NSJSONSerialization JSONObjectWithData:replyData options:NSJSONReadingMutableLeaves error:nil];
} else if ([data isKindOfClass:[NSDictionary class]]) {
replyDict = data;
}
if (replyDict == nil) {
return;
}
}
- (BOOL)shouldHandle:(NSString * _Nonnull)topic {
if ([topic isEqualToString:@"/properties/post"]) {
return YES;//返回YES,说明对此topic感兴趣,SDK会调用[listener onDownstream:data:]
}
return NO;
}
@end
self.testListner = [TestDownstreamListener new];//sdk不会strong持有此listener,开发者自己保证listener不被释放.
[[LKAppExpress sharedInstance]addDownStreamListener:YES listener:self.testListner]
长连接通道与账号绑定
绑定前需要确认账号已经登录,否则无法获取对应的iotToken。绑定时需要当前账号对应的iotToken来完成绑定。
#import <AlinkAppExpress/LKAppExpress.h>
#import <IMSAuthentication/IMSAuthentication.h>
#pragma mark - Channel
- (void)bindAccountWithChannel {
// 长连接通道绑定用户
IMSCredential *credential = [[IMSCredentialManager sharedManager] credential];
if (credential && credential.iotToken && ![credential isIotTokenExpired]) {
[self bindChannelWithToken:credential.iotToken];
} else {
[[IMSCredentialManager sharedManager] asyncRefreshCredential:^(NSError * _Nullable error, IMSCredential * _Nullable credential) {
if (credential && credential.iotToken) {
[self bindChannelWithToken:credential.iotToken];
} else {
IMSLogDebug(IMS_DEMO_TAG, @"移动推送请求iotToken失败:%@", error);
}
}];
}
}
- (void)bindChannelWithToken:(NSString *)token {
if (!token) {
return;
}
NSString *topic = @"/account/bind";
NSDictionary *params = @{
@"iotToken": token,
};
[[LKAppExpress sharedInstance] invokeWithTopic:topic
opts:nil
params:params
respHandler:^(LKAppExpResponse * _Nonnull response) {
if (![response successed]) {
IMSLogDebug(IMS_DEMO_TAG, @"长连接通道绑定账号失败");
}
}];
}
取消长连接通道与账号关联
取消关联需要在用户退出前进行操作;
#import <AlinkAppExpress/LKAppExpress.h>
#import <IMSAuthentication/IMSCredentialManager.h>
#pragma mark - 取消长连接通道关联
NSString *topic = @"/account/unbind";
[[LKAppExpress sharedInstance] invokeWithTopic:topic opts:nil params:@{} respHandler:^(LKAppExpResponse * _Nonnull response) {
if (![response successed]) {
IMSLifeLogVerbose(@"解绑长连接推送失败");
}
}];