MAC下搭建MQTT客戶端,測試釋出和訂閱話題
阿新 • • 發佈:2019-01-08
1. 下載MQTTKIT開源庫,編譯出你所需要的libMQTTKit.a和標頭檔案
MQTTKIT開源庫github下載地址:點這裡
下載好MQTTKIT開源庫後,編譯下(假如你想在模擬器上執行客戶端,可以直接編譯出一個模擬器版本的libMQTTKit.a和標頭檔案,反之編譯出一個手機客戶端版本)
編譯好的檔案截圖:
2. MQTT客戶端設計(連線伺服器,釋出話題,訂閱話題)
新建一個關於mqtt的xcode工程,然後將上面編譯好的libMQTTKit.a和標頭檔案拖到工程中去
然後新建一個類,用做連線測試用,具體程式碼如下:
(也可以參考這個github大神寫的demo:地址)
@implementation LJMQTTViewController - (void)dealloc { // disconnect the MQTT client [self.client disconnectWithCompletionHandler:^(NSUInteger code) { // The client is disconnected when this completion handler is called NSLog(@"MQTT is disconnected"); }]; } - (void)viewDidLoad { [super viewDidLoad]; [self initMqttSerive]; [self createBulletButton]; } - (void)createBulletButton { UIButton *_ljBtn = [[UIButton alloc]init]; [_ljBtn setFrame:CGRectMake((screenFrame.size.width - 150)/2.0, (screenFrame.size.height - 50)/2.0 + 120, 150, 50)]; _ljBtn.backgroundColor = [UIColor grayColor]; [_ljBtn setTitle:@"Bullet test" forState:UIControlStateNormal]; [_ljBtn addTarget:self action:@selector(ljBulletBtnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_ljBtn]; } - (void)initMqttSerive { // 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 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); [self subscribeTop:kTopic]; } }]; //獲取話題訊息回撥 //[self handleMqttMessage]; } - (void)ljBulletBtnClicked { [self publishString:@"test"]; } #pragma mark -- 訂閱話題 - (void)subscribeTop:(NSString*)topicStr { // Subscribe to the topic [self.client subscribe:topicStr withCompletionHandler:^(NSArray *grantedQos) { // The client is effectively subscribed to the topic when this completion handler is called NSLog(@"subscribed to topic %@", kTopic); }]; } #pragma mark -- 釋出 - (void)publishString:(NSString*)payload { // 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 } #pragma mark -- received message - (void)handleMqttMessage { // define the handler that will be called when MQTT messages are received by the client [self.client setMessageHandler:^(MQTTMessage *message) { NSLog(@"received message %@", message.payloadString); }]; }
此時可以看到控制檯的列印資訊,表示連線伺服器成功,此時就可以釋出訊息和訂閱訊息了
從上面的log可以看出,當你連線伺服器成功後,伺服器會給你返回一個唯一的id,當服務端想給你推訊息時,可以根據你的這個唯一id來推給你
我此處訂閱的話題是“#”,它代表伺服器上所有的話題訊息,我都可以接收到。
3. 通過PHP網頁來向客戶端傳送一個話題(此處的伺服器就是上一篇搭建的mosquitto)
PHP傳送訊息的截圖(http://localhost/PhpM/index.php):
PHP的MQTT環境搭建見我的這篇文章:地址
IOS MQTT客戶端接收到的訊息截圖: