日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mqtt协议IOS端移植3

發布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mqtt协议IOS端移植3 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ServerMqFramework.h

#import "MqttFramework.h"@interface ServerMqFramework : MqttFramework/*** @brief 得到模塊控制器的句柄單例** @param [in] N/A* @param [out] N/A* @return void* @note*/+(ServerMqFramework*)getMQttServerFrameInstance;- (int)callBusinessProcessEX:(NSString *)capabilityId withMessageType:(NSString *)MessageType WithMessage:(id)messageVector withTopic:(NSString *)topic;@end


ServerMqFramework.m


#import "ServerMqFramework.h" #import "getMacAddress.h"@implementation ServerMqFramework/*** @brief 得到模塊控制器的句柄單例** @param [in] N/A* @param [out] N/A* @return void* @note*/static ServerMqFramework *mqttInstance = nil; +(ServerMqFramework*)getMQttServerFrameInstance {@synchronized(self){if(mqttInstance==nil){mqttInstance=[[self alloc]init];}}return mqttInstance; }#pragma --mark BusinessModuleProtocol delegate - (int)initBusinessModule:(BusinessModuleInfo*)info {info.businessModuleIdEX = @"MqttServerFrameWork";//businessFrameworks_ = info.businessFramework;return 0; }//指定發送到遠程的響應接收者的主題 (實現宏觀上的點對點傳輸) //2012/11/30 modify- (int)callBusinessProcessEX:(NSString *)capabilityId withMessageType:(NSString *)MessageType WithMessage:(id)messageVector withTopic:(NSString *)topic {NSLog(@"server capabilityId = %@ topic = %@ messageVector = %@",capabilityId,MessageType,messageVector);AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];MQTTClient *serverMQTTClient = [app serverMqClient];NSString *sendStr = [[NSString alloc] initWithData:[XmlAdept makeMqttXML:MessageType withDictionary:(NSDictionary *)messageVector] encoding:NSUTF8StringEncoding];// /****************去掉最后換行符********************/int index = sendStr.length-1;sendStr = [sendStr substringToIndex:index];NSString *string = [NSString stringWithFormat:@"**%@**",sendStr];NSLog(@"server senderMesg = %@",string);[serverMQTTClient publishString:sendStr toTopic:topic retain:NO];return 0; }-(void)requestResult:(NSString *)topic withData:(id)resultData {NSLog(@"遠程mq服務器發送業務廣播 給監聽者");[businessFrameworks_ broadcastBusinessNotifyEX:topic withInParam:resultData]; }/*** @brief 定義mqtt消息主題** @param [in] N/A* @param [out] N/A* @return void* @note*/-(void)productTheme:(NSString *)theme {NSLog(@"server theme = %@",theme);AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];MQTTClient *mosq = [app serverMqClient];[mosq setHost:Mqtt_Server_URL];[mosq connect];[mosq subscribe:theme]; }/*** @brief 重連mqtt服務器** @param [in] N/A* @param [out] N/A* @return void* @note*/ -(void)reconnectMqtt {AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];MQTTClient *mosq = [app serverMqClient];[mosq setHost:Mqtt_Server_URL];[mosq reconnect];}#pragma --mark mosquittoclientDelegate- (void) didConnect:(NSUInteger)code {if (code == 0){NSLog(@"連接遠程Mqtt服務器返回碼為:%d 連接mqtt成功",code);}else{NSLog(@"連接遠程Mqtt服務器失敗");}}- (void) didDisconnect {NSLog(@"Server mqtt disconnect!");AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];MQTTClient *mosq = [app serverMqClient];//斷開后自己主動重連//3g或wifi任一可達就重連if ([Common IsEnable3G] || [Common IsEnableWIFI]){[mosq reconnect]; //遠程MQTT服務器端重連NSString *serverTheme = getMacAddress();[mosq subscribe:serverTheme]; //又一次訂閱主題}}//處理各個主題相應的message- (void) didReceiveMessage: (NSString*)message topic:(NSString*)topic {NSLog(@" serverMqtt %@ => %@", topic, message);NSArray *resultArr = [[NSArray alloc]initWithObjects:message, nil];NSArray *list = [XmlAdept mqttParseMessageNode:resultArr];NSString *type = [[list objectAtIndex:0] objectForKey:@"type"];[self requestResult:type withData:list];}/*** @brief 給遠程mqtt服務器發送消息 對外接口** @param [in] N/A* @param [out] N/A* @return void* @note*/+(void)publishMessage:(NSString *)message withTopicType:(NSString *)topicType {AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];MQTTClient *mosq = [app serverMqClient];[mosq publishString:message toTopic:topicType retain:NO]; //消息的retain類型設置為NO讓其不在消息緩沖池中保留 }-(void)dealloc {[super dealloc]; }@end

轉載于:https://www.cnblogs.com/gccbuaa/p/7214395.html

總結

以上是生活随笔為你收集整理的Mqtt协议IOS端移植3的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。