iOS開發(fā)之MQTT探究
1、 什么是MQTT?
MQTT(MessageQueueing Telemetry Transport Protocol)的全稱是消息隊(duì)列遙感傳輸協(xié)議的縮寫,是由IBM公司推出的一種基于輕量級(jí)代理的發(fā)布/訂閱模式的消息傳輸協(xié)議,運(yùn)行在TCP協(xié)議棧之上,為其提供有序、可靠、雙向連接的網(wǎng)絡(luò)連接保證。由于其開放、簡(jiǎn)單和易于實(shí)現(xiàn)所以能夠應(yīng)用在資源受限的環(huán)境中,對(duì)于M2M和物聯(lián)網(wǎng)應(yīng)用程序來(lái)說(shuō)是一個(gè)相當(dāng)不錯(cuò)的選擇。
2、 為什么要用MQTT?
MQTT協(xié)議是針對(duì)如下情況設(shè)計(jì)的:
M2M(Machine to Machine) communication,機(jī)器端到端通信,比如傳感器之間的數(shù)據(jù)通訊 因?yàn)槭荕achine to Machine,需要考慮: Machine,或者叫設(shè)備,比如溫度傳感器,硬件能力很弱,協(xié)議要考慮盡量小的資源消耗,比如計(jì)算能力和存儲(chǔ)等 M2M可能是無(wú)線連接,網(wǎng)絡(luò)不穩(wěn)定,帶寬也比較小
MQTT的特點(diǎn):
1.發(fā)布/訂閱消息模式,提供一對(duì)多的消息發(fā)布,解除應(yīng)用程序耦合。這一點(diǎn)很類似于1. 這里是列表文本XMPP,但是MQTT的信息冗余遠(yuǎn)小于XMPP.
2.對(duì)負(fù)載內(nèi)容屏蔽的消息傳輸。
3.使用TCP/IP提供網(wǎng)絡(luò)連接。主流的MQTT是基于TCP連接進(jìn)行數(shù)據(jù)推送的,但是同樣有基于UDP的版本,叫做MQTT-SN。這兩種版本由于基于不同的連接方式,優(yōu)缺點(diǎn)自然也就各有不同了。
4.三種消息傳輸方式QoS:
- 0代表“至多一次”,消息發(fā)布完全依賴底層 TCP/IP 網(wǎng)絡(luò)。會(huì)發(fā)生消息丟失或重復(fù)。這一級(jí)別可用于如下情況,環(huán)境傳感器數(shù)據(jù),丟失一次讀記錄無(wú)所謂,因?yàn)椴痪煤筮€會(huì)有第二次發(fā)送。
- 1代表“至少一次”,確保消息到達(dá),但消息重復(fù)可能會(huì)發(fā)生。
- 2代表“只有一次”,確保消息到達(dá)一次。這一級(jí)別可用于如下情況,在計(jì)費(fèi)系統(tǒng)中,消息重復(fù)或丟失會(huì)導(dǎo)致不正確的結(jié)果。 備注:由于服務(wù)端采用Mosca實(shí)現(xiàn),Mosca目前只支持到QoS 1
- 如果發(fā)送的是臨時(shí)的消息,例如給某topic所有在線的設(shè)備發(fā)送一條消息,丟失的話也無(wú)所謂,0就可以了(客戶端登錄的時(shí)候要指明支持的QoS級(jí)別,同時(shí)發(fā)送消息的時(shí)候也要指明這條消息支持的QoS級(jí)別),如果需要客戶端保證能接收消息,需要指定QoS為1,如果同時(shí)需要加入客戶端不在線也要能接收到消息,那么客戶端登錄的時(shí)候要指定session的有效性,接收離線消息需要指定服務(wù)端要保留客戶端的session狀態(tài)。
- mqtt基于訂閱者模型架構(gòu),客戶端如果互相通信,必須在同一訂閱主題下,即都訂閱了同一個(gè)topic,客戶端之間是沒(méi)辦法直接通訊的。訂閱模型顯而易見的好處是群發(fā)消息的話只需要發(fā)布到topic,所有訂閱了這個(gè)topic的客戶端就可以接收到消息了。
- 發(fā)送消息必須發(fā)送到某個(gè)topic,重點(diǎn)說(shuō)明的是不管客戶端是否訂閱了該topic都可以向topic發(fā)送了消息,還有如果客戶端訂閱了該主題,那么自己發(fā)送的消息也會(huì)接收到。
5.小型傳輸,開銷很小(固定長(zhǎng)度的頭部是2字節(jié)),協(xié)議交換最小化,以降低網(wǎng)絡(luò)流量。這就是為什么在介紹里說(shuō)它非常適合“在物聯(lián)網(wǎng)領(lǐng)域,傳感器與服務(wù)器的通信,信息的收集”,要知道嵌入式設(shè)備的運(yùn)算能力和帶寬都相對(duì)薄弱,使用這種協(xié)議來(lái)傳遞消息再適合不過(guò)了。
6.使用Last Will和Testament特性通知有關(guān)各方客戶端異常中斷的機(jī)制。Last Will:即遺言機(jī)制,用于通知同一主題下的其他設(shè)備發(fā)送遺言的設(shè)備已經(jīng)斷開了連接。Testament:遺囑機(jī)制,功能類似于Last Will 。
3、 怎么使用MQTT
在mac上搭建MQTT服務(wù)器
- $ brew install mosquitto
等待下載完成,服務(wù)會(huì)自動(dòng)運(yùn)行起來(lái)
- mosquitto has been installed with a default configuration file.
- You can make changes to the configuration by editing:
- /usr/local/etc/mosquitto/mosquitto.conf
- To have launchd start mosquitto now and restart at login:
- brew services start mosquitto
- Or, if you don't want/need a background service you can just run:
- mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
iOS client注冊(cè)
- #import "ViewController.h"
- #define kMQTTServerHost @"iot.eclipse.org"
- #define kTopic @"MQTTExample/Message"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UILabel *showMessage;
- @property (nonatomic, strong) MQTTClient *client;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //1.在app登錄后,后臺(tái)返回 name+password+topic
- //2.name+password用于連接主機(jī)
- //3.topic 用于訂閱主題
- UILabel *tempShowMessage = self.showMessage;
- NSString *clientID = [UIDevice currentDevice].identifierForVendor.UUIDString;
- self.client = [[MQTTClient alloc] initWithClientId:clientID];
- //連接服務(wù)器 連接后,會(huì)通過(guò)block將連接結(jié)果code返回,然后執(zhí)行此段代碼塊
- //這個(gè)接口是修改過(guò)后的接口,修改后拋出了name+password
- [self.client connectToHost:kMQTTServerHost andName:@"cbt" andPassword:@"1223" completionHandler:^(MQTTConnectionReturnCode code) {
- if (code == ConnectionAccepted)//連接成功
- {
- // 訂閱
- [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);
- NSLog(@"return:%@",grantedQos);
- }];
- }
- }];
- //MQTTMessage 里面的數(shù)據(jù)接收到的是二進(jìn)制,這里框架將其封裝成了字符串
- [self.client setMessageHandler:^(MQTTMessage* message)
- {
- dispatch_async(dispatch_get_main_queue(), ^{
- //接收到消息,更新界面時(shí)需要切換回主線程
- tempShowMessage.text= message.payloadString;
- });
- }];
- }
- - (void)dealloc8
- {
- // disconnect the MQTT client
- [self.client disconnectWithCompletionHandler:^(NSUInteger code)
- {
- // The client is disconnected when this completion handler is called
- NSLog(@"MQTT is disconnected");
- }];
- }
- @end
server向client推送消息
- #import "ViewController.h"
- #import "MQTTKit.h"
- #define kMQTTServerHost @"iot.eclipse.org"
- #define kTopic @"MQTTExample/Message"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UITextField *pushMessage;
- @property (nonatomic, strong) MQTTClient *client;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSString *clientID = [UIDevice currentDevice].identifierForVendor.UUIDString;
- self.client = [[MQTTClient alloc] initWithClientId:clientID];
- [self.client connectToHost:kMQTTServerHost andName:@"cbt" andPassword:@"1223" completionHandler:^(MQTTConnectionReturnCode code) {
- if (code == ConnectionAccepted)
- {
- NSLog(@"服務(wù)器啟動(dòng)成功");
- }
- }];
- }
- - (IBAction)push:(id)sender {
- NSString* payload = self.pushMessage.text;
- [self.client publishString:payload
- toTopic:kTopic
- withQos:AtMostOnce
- retain:YES
- completionHandler:nil];
- NSLog(@"推送內(nèi)容:%@",payload);
- }