MQTTKit使用

1、 ** MQTTKit ** `已经不更新 但是基本使用没问题

> pod 'MQTTKit'   

头文件

> #import <MQTTKit.h>

#define WEAKSELF  __typeof(&*self) __weak weakSelf = self;

@property (nonatomic, strong) MQTTClient *client;

初始化 链接


WEAKSELF

    NSString *clientID = @"测试client - 必须是全局唯一的id ";

    MQTTClient *client = [[MQTTClient alloc] initWithClientId:StrFormat(@"%@", clientID)];

    client.username = @"username";

    client.password = @"password";

    client.cleanSession = false;

    client.keepAlive = 20;

    client.port = 11883;// 端口号 根据服务端 选择

    self.client = client;

    // 链接MQTT

    [client connectToHost:@"链接的MQTT的URL" completionHandler:^(MQTTConnectionReturnCode code) {

        if (code == ConnectionAccepted) {

            NSLog(@"链接MQTT 成功 😁😁😁");

            // 链接成功  订阅相对应的主题

        [weakSelf.client subscribe:@"你需要订阅的主题" withQos:AtLeastOnce completionHandler:^(NSArray *grantedQos) {

            DLog(@"订阅 返回 %@",grantedQos);

        }];

        }else if (code == ConnectionRefusedBadUserNameOrPassword){

            NSLog(@"MQTT 账号或验证码错误");

        } else if (code == ConnectionRefusedUnacceptableProtocolVersion){

            NSLog(@"MQTT 不可接受的协议");

        }else if (code == ConnectionRefusedIdentiferRejected){

            NSLog(@"MQTT不认可");

        }else if (code == ConnectionRefusedServerUnavailable){

            NSLog(@"MQTT拒绝链接");

        }else {

            NSLog(@"MQTT 未授权");

        }

    }];

// 接收消息体

    client.messageHandler = ^(MQTTMessage *message) {

        NSString *jsonStr = [[NSString alloc] initWithData:message.payload encoding:NSUTF8StringEncoding];

        NSLog(@"EasyMqttService mqtt connect success  %@",jsonStr);

    };

订阅主题


// 方法 封装 可外部调用

-(void)subscribeType:(NSString *)example{

    // 订阅主题

    [self.client subscribe:@"你需要订阅的主题" withQos:AtMostOnce completionHandler:^(NSArray *grantedQos) {

        NSLog(@"订阅 返回 %@",grantedQos);

    }];

}

关闭MQTTKit


-(void)closeMQTTClient{

    WEAKSELF

    [self.client disconnectWithCompletionHandler:^(NSUInteger code) {

        // The client is disconnected when this completion handler is called

        NSLog(@"MQTT client is disconnected");

        [weakSelf.client unsubscribe:@"已经订阅的主题" withCompletionHandler:^{

            NSLog(@"取消订阅");

        }];

    }];

}

发送消息


  [self.client publishString:postMsg toTopic:@"发送消息的主题 根据服务端定"  withQos:AtLeastOnce retain:NO completionHandler:^(int mid) {

        if (cmd != METHOD_SOCKET_CHAT_TO) {

            NSLog(@"发送消息 返回 %d",mid);

        }

    }];

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,235评论 0 15
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 5,859评论 0 9
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,545评论 1 32
  • 故乡是一个不大的小城,隐没于武陵山脉的大山深处,似一个小盆地被大山环抱着。春天,周围大山上山花烂漫在碧绿之间,梨树...
    追梦钰儿阅读 929评论 13 14
  • 悄悄的打开笔记本,调好台灯的位置,回头看了一眼睡梦中的大宝,开始了我的隔三差五的点点滴滴的记录。 自从有了...
    果果长点心吧阅读 314评论 0 0

友情链接更多精彩内容