iOS --- MQTTClient

----- 更新 目前使用了 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;
}

当绑定完成之后,会打印出以下的信息。


WX20180725-172624.png

第三步:获取状态

#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];
        
    }
    
}

其他

目前使用下来没遇到太大问题,基本能满足项目所需。

Demo下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容

  • 什么是MQTT MQTT(消息队列遥测传输),基于发布/订阅的消息协议。MQTT工作在TCP/IP协议族上,是为硬...
    MoreFish阅读 2,901评论 1 8
  • 很久没有写东西了,半年搞1个半app,2个ipad项目,人已疯。。。。 今天在重构代码,总结一下:MQTT的使用和...
    今年我25阅读 58,935评论 55 141
  • MQTT是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分。该协议支持所有平台,几乎可以把所有联网物品...
    烟雨酱南阅读 1,968评论 1 2
  • 前言 本文会用实例的方式,将iOS各种IM的方案都简单的实现一遍。并且提供一些选型、实现细节以及优化的建议。 注:...
    maTianHong阅读 2,411评论 4 12
  • 最近公司在做车联网,想到用MQTT实现多通道连接订阅主题,能够高效的进行消息传递。实现手机端、硬件、网站、后台多端...
    低调的雅痞先生阅读 4,810评论 3 5