因為公司業(yè)務(wù)需要滥崩,需要app能夠?qū)崟r監(jiān)聽服務(wù)器傳過來信息來更新UI界面闷盔,這和傳統(tǒng)app向服務(wù)器來發(fā)送請求不同,所以在后臺的建議下采用了MQTT。而MQTTKit是我找到比較輕量級消约,比較清新的框架,但是這個框架存在一些問題员帮。這篇文章就是來解決這些問題的或粮。
廢話不多說,直接進入主題集侯。
app端只需要修改這個文件,MQTTKitComeOn帜消,下面那個只是demo可以讓你看到效果棠枉,但是我們app端只需要MQTTKitComeOn就可以完成連接操作。
如果想看到效果的話泡挺,可以用手機模擬開一個客戶端MQTTKitComeOn辈讶,用模擬器開一個MQTTServer。
我們客戶端這邊需要做的是娄猫,根據(jù)設(shè)備的唯一標示UUID來創(chuàng)建MQTTClient贱除,并且通過MQTTClient來連接 服務(wù)器主機(注意:這里是主機)。
NSString *clientID = [UIDevice currentDevice].identifierForVendor.UUIDString;
self.client = [[MQTTClient alloc] initWithClientId:clientID];
這里我們需要四個參數(shù)來完成連接媳溺,name月幌,password,topic(主題)悬蔽,port(端口)
扯躺。(這個也是你們后臺需要的)
ps:name + password + port是用戶在登錄后 服務(wù)器返回來的
但是在進入MQTTKit內(nèi)部代碼之后發(fā)現(xiàn),MQTTKit壓根就沒有提供這個接口蝎困,而是直接將這幾個參數(shù)封裝在.m里面并且寫死了录语。
所以這時候,我將這四個參數(shù)修改了接口禾乘。下面這兩個都需要修改成下面這樣子
MQTTKit.h
- (void)connectToHost:(NSString *)host andName:(NSString *)name andPassword:(NSString *)password andPort:(int)port completionHandler:(void (^)(MQTTConnectionReturnCode code))completionHandler;
- (void) connectWithCompletionHandler:(NSString *)name andPassword:(NSString *)password andPort:(int)port andCallBack:(void (^)(MQTTConnectionReturnCode code))completionHandler;
MQTTKit.m
- (void)connectToHost:(NSString *)host andName:(NSString *)name andPassword:(NSString *)password andPort:(int)port
completionHandler:(void (^)(MQTTConnectionReturnCode code))completionHandler {
self.host = host;
// [self connectWithCompletionHandler:completionHandler];
[self connectWithCompletionHandler:name andPassword:password andPort:port andCallBack:completionHandler];
}
- (void)connectWithCompletionHandler:(NSString *)name andPassword:(NSString *)password andPort:(int)port andCallBack:(void (^)(MQTTConnectionReturnCode))completionHandler{
self.connectionCompletionHandler = completionHandler;
const char *cstrHost = [self.host cStringUsingEncoding:NSASCIIStringEncoding];
const char *cstrUsername = NULL, *cstrPassword = NULL;
self.username = name;
//
self.password = password;
if (self.username){
cstrUsername = [self.username cStringUsingEncoding:NSUTF8StringEncoding];
}
if (self.password){
cstrPassword = [self.password cStringUsingEncoding:NSUTF8StringEncoding];
}
mosquitto_username_pw_set(mosq, cstrUsername, cstrPassword);
// printf("name is:%s\n,password is:%s\n",cstrUsername,cstrPassword);
mosquitto_reconnect_delay_set(mosq, self.reconnectDelay, self.reconnectDelayMax, self.reconnectExponentialBackoff);
mosquitto_connect(mosq, cstrHost, port, self.keepAlive);
dispatch_async(self.queue, ^{
LogDebug(@"start mosquitto loop on %@", self.queue);
mosquitto_loop_forever(mosq, -1, 1);
LogDebug(@"end mosquitto loop on %@", self.queue);
});
}
代碼按照上面修改后澎埠,我們來到正式調(diào)用的地方來看看怎么使用
#pragma mark -開啟mqtt服務(wù)
- (void)MQTTClientStart{
//這里只是封裝了一個單利MQTTClient
MQTTClient * mqttManager = [WXMQTTManager sharedMQTTClientManasger];
//userCachePath是歸檔地址
WXUser * user = [NSKeyedUnarchiver unarchiveObjectWithFile:userCachePath];
//相關(guān)數(shù)據(jù)都存在的話
if (user && [user.mqtt_password length] > 0 && [user.mqtt_username length] > 0 && [user.mqtt_topic length]> 0) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[mqttManager connectToHost:WX_SERVER_HOST andName:user.mqtt_username andPassword:user.mqtt_password andPort:WX_SERVER_PORT completionHandler:^(MQTTConnectionReturnCode code) {
if (code == ConnectionAccepted)//連接成功
{
// 訂閱
[mqttManager subscribe:user.mqtt_topic withCompletionHandler:^(NSArray *grantedQos) {
NSLog(@"return:%@",grantedQos);
}];
}else{
DDLogInfo(@"出錯了 code-->%ld",code);
}
}];
//監(jiān)聽接收數(shù)據(jù)
[mqttManager setMessageHandler:^(MQTTMessage* message)
{
dispatch_async(dispatch_get_main_queue(), ^{
//接收到消息,更新界面時需要切換回主線程
// tempShowMessage.text= message.payloadString;
DDLogInfo(@"%@",message.payloadString);
});
}];
});
}
}
- 連接操作需要放在子線程中始藕,不然會阻塞主線程
- 因為對第三方框架進行了修改蒲稳,不要再使用pods來導(dǎo)入框架
這個是github上面地址,兄弟們以后直接在這里下載即可
https://github.com/knightSaber/MQTTServer