MQTT

简介

MQTT是一个物联网传输协议,它被设计用于轻量级的发布/订阅式消息传输,旨在为低带宽和不稳定的网络环境中的物联网设备提供可靠的网络服务。MQTT是专门针对物联网开发的轻量级传输协议。MQTT协议针对低带宽网络,低计算能力的设备,做了特殊的优化,使得其能适应各种物联网应用场景。目前MQTT拥有各种平台和设备上的客户端,已经形成了初步的生态系统。

http://www.infoq.com/cn/news/2014/12/mqtt-ibm-iot?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global
不过个人觉得还是读人家的写的比较能深刻理解http://mqtt.org

优势

MQTT的设计思想是开源、可靠、轻巧、简单,MQTT的传输格式非常精小,最小的数据包只有2个比特,且无应用消息头。MQTT可以保证消息的可靠性,它包括三种不同的服务质量(最多只传一次、最少被传一次、一次且只传一次),如果客户端意外掉线,可以使用“遗愿”发布一条消息,同时支持持久订阅。MQTT在物联网以及移动应用中的优势有:
1.可靠传输:MQTT可以保证消息可靠安全的传输,并可以与企业应用简易集成。
2.消息推送:支持消息实时通知、丰富的推送内容、灵活的Pub-Sub以及消息存储和过滤。
3.低带宽、低耗能、低成本。占用移动应用程序带宽小,并且带宽利用率高,耗电量较少。

GitHub上面有一些有关MQTT的demo,饶了很远才发现又回到了起点,首先遇到一个新技术,我们必须知道他是用来干什么的,然后需要知道他是什么,最后怎么用。当然也需要时间的积累。
https://github.com/mobile-web-messaging/MQTTKit

https://github.com/jmesnil/MQTTKit

代码

说那么多也是然并卵,上代码。
1、首先,需要MQTT的框架
用cocoaPod引入时非常方便的,直接pod ‘MATTKit’即可下载,
在你要用的地方引入

#import "MQTTKit.h"
#define kMWTTServerHost @"iot.eclipse.org"
#define kTopic @"MATTExample/LED"

服务器Host是git上demo里面的,topic也是上文网址中工程里面的。可以测试使用。

// create the MQTT client with an unique identifier
    NSString *clientID = [UIDevice currentDevice].identifierForVendor.UUIDString;
    self.client = [[MQTTClient alloc] initWithClientId:clientID];

    // keep a reference on the switch to avoid having a reference to self in the
    // block below (retain/release cycle, blah blah blah)
    UISwitch *subSwitch = self.subscribedSwitch;

    // define the handler that will be called when MQTT messages are received by the client
    [self.client setMessageHandler:^(MQTTMessage *message) {
        // extract the switch status from the message payload
        BOOL on = [message.payloadString boolValue];


        // the MQTTClientDelegate methods are called from a GCD queue.
        // Any update to the UI must be done on the main queue  任何UI的更新必须在主线程中
        dispatch_async(dispatch_get_main_queue(), ^{
            [subSwitch setOn:on animated:YES];
        });
    }];

    // connect the MQTT client
    [self.client connectToHost:kMQTTServerHost completionHandler:^(MQTTConnectionReturnCode code) {
        if (code == ConnectionAccepted) {
            // The client is connected when this completion handler is called
            NSLog(@"client is connected with id %@", clientID);
            // Subscribe to the topic
            [self.client subscribe:kTopic withCompletionHandler:^(NSArray *grantedQos) {
                // The client is effectively subscribed to the topic when this completion handler is called
                NSLog(@"subscribed to topic %@", kTopic);
            }];
        }
    }];
    // Do any additional setup after loading the view, typically from a nib.

OK,全部摘自git测试demo中,并添加了一些自己的注释。

//发布主题
// This method is called when the "published LED" switch status changes 发布状态改变的主题
- (IBAction)publish:(id)sender {
    BOOL on = [sender isOn];
    NSString *payload = [NSNumber numberWithBool:on].stringValue;

    // use the MQTT client to send a message with the switch status to the topic发送开关状态的消息到主题
    [self.client publishString:payload
                       toTopic:kTopic
                       withQos:AtMostOnce
                        retain:YES
             completionHandler:nil];
    // we passed nil to the completionHandler as we are not interested to know
    // when the message was effectively sent
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值