----- 更新 目前使用了 MQTTSessionManage 类 来代替之前的 MQTTSession -----
在最近的一个项目中,使用到了mqtt协议,并且需要SSL加密单向验证,故把自己的所写记录下。
第一步:安装
目前,使用的是Pod生成的MQTTClient第三方库,直接下载就行 pod 'MQTTClient'
第二步:绑定
绑定前需要设置几个属性,主要有:
帐号、密码、clientId、ip、端口。
其次,还要注意这个库是没有连接中断自动重连的。所以需要监听他的状态。
#pragma mark - 绑定
- (void)bindWithUserName:(NSString *)username password:(NSString *)password cliendId:(NSString *)cliendId isSSL:(BOOL)isSSL{
self.username = username;
self.password = password;
self.cliendId = cliendId;
self.SSL = isSSL;
/*
self.mySession = [[MQTTSession alloc]initWithClientId:self.cliendId userName:self.username password:self.password keepAlive:60 cleanSession:YES will:NO willTopic:nil willMsg:nil willQoS:MQTTQosLevelAtLeastOnce willRetainFlag:NO protocolLevel:4 queue:dispatch_get_main_queue() securityPolicy:[self customSecurityPolicy] certificates:nil];
self.isDiscontent = NO;
self.mySession.delegate = self;
[self.mySession connectToHost:AddressOfMQTTServer port:self.isSSL?PortOfMQTTServerWithSSL:PortOfMQTTServer usingSSL:isSSL];
[self.mySession addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
*/
[self.mySessionManager connectTo:AddressOfMQTTServer
port:self.isSSL?PortOfMQTTServerWithSSL:PortOfMQTTServer
tls:self.isSSL
keepalive:60
clean:YES
auth:YES
user:self.username
pass:self.password
will:NO
willTopic:nil
willMsg:nil
willQos:MQTTQosLevelAtLeastOnce
willRetainFlag:NO
withClientId:self.cliendId
securityPolicy:[self customSecurityPolicy]
certificates:nil
protocolLevel:4
connectHandler:nil];
self.isDiscontent = NO;
self.mySessionManager.subscriptions = self.subedDict;
}
- (MQTTSSLSecurityPolicy *)customSecurityPolicy
{
MQTTSSLSecurityPolicy *securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
securityPolicy.validatesCertificateChain = YES;
securityPolicy.validatesDomainName = NO;
return securityPolicy;
}
当绑定完成之后,会打印出以下的信息。
第三步:获取状态
#pragma mark ---- 状态
- (void)sessionManager:(MQTTSessionManager *)sessionManager didChangeState:(MQTTSessionManagerState)newState {
switch (newState) {
case MQTTSessionManagerStateConnected:
NSLog(@"eventCode -- 连接成功");
break;
case MQTTSessionManagerStateConnecting:
NSLog(@"eventCode -- 连接中");
break;
case MQTTSessionManagerStateClosed:
NSLog(@"eventCode -- 连接被关闭");
break;
case MQTTSessionManagerStateError:
NSLog(@"eventCode -- 连接错误");
break;
case MQTTSessionManagerStateClosing:
NSLog(@"eventCode -- 关闭中");
break;
case MQTTSessionManagerStateStarting:
NSLog(@"eventCode -- 连接开始");
break;
default:
break;
}
}
第四步:订阅命令
#pragma mark - 订阅
- (void)subscribeTopic:(NSString *)topic handler:(SubscribeTopicHandler)handler{
NSLog(@"当前需要订阅-------- topic = %@",topic);
if (![self.subedDict.allKeys containsObject:topic]) {
[self.subedDict setObject:[NSNumber numberWithLong:MQTTQosLevelAtLeastOnce] forKey:topic];
NSLog(@"订阅字典 ----------- = %@",self.subedDict);
self.mySessionManager.subscriptions = self.subedDict;
}
else {
NSLog(@"已经存在,不用订阅");
}
取消订阅
#pragma mark - 取消订阅
- (void)unsubscribeTopic:(NSString *)topic {
NSLog(@"当前需要取消订阅-------- topic = %@",topic);
if ([self.subedDict.allKeys containsObject:topic]) {
[self.subedDict removeObjectForKey:topic];
NSLog(@"更新之后的订阅字典 ----------- = %@",self.subedDict);
self.mySessionManager.subscriptions = self.subedDict;
}
else {
NSLog(@"不存在,无需取消");
}
}
发布消息
#pragma mark - 发布消息
- (void)sendDataToTopic:(NSString *)topic dict:(NSDictionary *)dict {
NSLog(@"发送命令 topic = %@ dict = %@",topic,dict);
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
[self.mySessionManager sendData:data topic:topic qos:MQTTQosLevelAtLeastOnce retain:NO];
}
数据接收回调
#pragma mark MQTTSessionManagerDelegate
- (void)handleMessage:(NSData *)data onTopic:(NSString *)topic retained:(BOOL)retained {
if (self.delegate && [self.delegate respondsToSelector:@selector(MQTTClientModel_handleMessage:onTopic:retained:)]) {
[self.delegate MQTTClientModel_handleMessage:data onTopic:topic retained:retained];
}
}
主动断开
- (void)disconnect {
self.isDiscontent = YES;
// self.isContented = NO;
[self.mySessionManager disconnectWithDisconnectHandler:^(NSError *error) {
NSLog(@"断开连接 error = %@",[error description]);
}];
[self.mySessionManager setDelegate:nil];
self.mySessionManager = nil;
}
重连
- (void)reConnect {
if (self.mySessionManager && self.mySessionManager.port) {
self.mySessionManager.delegate = self;
self.isDiscontent = NO;
[self.mySessionManager connectToLast:^(NSError *error) {
NSLog(@"重新连接 error = %@",[error description]);
}];
self.mySessionManager.subscriptions = self.subedDict;
}
else {
[self bindWithUserName:self.username password:self.password cliendId:self.cliendId isSSL:self.isSSL];
}
}
其他
目前使用下来没遇到太大问题,基本能满足项目所需。