因为公司业务需要,需要app能够实时监听服务器传过来信息来更新UI界面,这和传统app向服务器来发送请求不同,所以在后台的建议下采用了MQTT。而MQTTKit是我找到比较轻量级,比较清新的框架,但是这个框架存在一些问题。这篇文章就是来解决这些问题的。 废话不多说,直接进入主题。
94F63320-B2C2-4D19-AD83-C62CE9D1AEA3.png
app端只需要修改这个文件,MQTTKitComeOn,下面那个只是demo可以让你看到效果,但是我们app端只需要MQTTKitComeOn就可以完成连接操作。 如果想看到效果的话,可以用手机模拟开一个客户端MQTTKitComeOn,用模拟器开一个MQTTServer。 我们客户端这边需要做的是,根据设备的唯一标示UUID来创建MQTTClient,并且通过MQTTClient来连接 服务器主机(注意:这里是主机)。 NSString *clientID = [UIDevice currentDevice].identifierForVendor.UUIDString; self.client = [[MQTTClient alloc] initWithClientId:clientID]; 这里我们需要四个参数来完成连接,name,password,topic(主题),port(端口)。(这个也是你们后台需要的) ps:name + password + port是用户在登录后 服务器返回来的 但是在进入MQTTKit内部代码之后发现,MQTTKit压根就没有提供这个接口,而是直接将这几个参数封装在.m里面并且写死了。 所以这时候,我将这四个参数修改了接口。下面这两个都需要修改成下面这样子 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); }); } 代码按照上面修改后,我们来到正式调用的地方来看看怎么使用 #pragma mark -开启mqtt服务 - (void)MQTTClientStart{ //这里只是封装了一个单利MQTTClient MQTTClient * mqttManager = [WXMQTTManager sharedMQTTClientManasger]; //userCachePath是归档地址 WXUser * user = [NSKeyedUnarchiver unarchiveObjectWithFile:userCachePath]; //相关数据都存在的话 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); } }]; //监听接收数据 [mqttManager setMessageHandler:^(MQTTMessage* message) { dispatch_async(dispatch_get_main_queue(), ^{ //接收到消息,更新界面时需要切换回主线程 // tempShowMessage.text= message.payloadString; DDLogInfo(@"%@",message.payloadString); }); }]; }); } }
(责任编辑:最模板) |