MQTT protocol iOS Port porting 3

Source: Internet
Author: User

ServerMqFramework.h

#import "MqttFramework.h" @interface servermqframework:mqttframework/** * @brief  get the handle of the module controller single example * @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 get the handle of the module controller single example * @ param [in] n/A * @param [out] n/A * @return void * @note */static servermqframework *mqttinstance = nil;+ (servermqframewor        k*) getmqttserverframeinstance{@synchronized (self) {if (Mqttinstance==nil) {mqttinstance=[[self alloc]init]; }}return mqttinstance;} #pragma--mark businessmoduleprotocol delegate-(int) Initbusinessmodule: (businessmoduleinfo*) info{info.business    Moduleidex = @ "mqttserverframework";//Businessframeworks_ = Info.businessframework; return 0;} Specifies the topic to send to the remote response recipient (for Point-to-point transmission on the macro)//2012/11/30 modify-(int) Callbusinessprocessex: (NSString *) Capabilityid WITHMESSAG EType: (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];    /**************** remove last line break ********************/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 (@ "Remote MQ server sends business broadcast to listener"); [Businessframeworks_ broadcastbusinessnotifyex:topic withinparam:resultdata];} /** * @brief define MQTT Message Subject * * @param [in] n/A * @param [out] n/A * @return void * @note */-(void) Producttheme: (NSString *) th    eme{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 re-connect MQTT server * * @param [in] n/A * @param [out] n/A * @return void * @note */-(void) reconnectmqtt{Appdelegat    E *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 (@ "Connect remote MQ    TT Server return code is:%d connection mqtt successful ", code);    } else {NSLog (@ "Connection to remote MQTT server failed");    }}-(void) Diddisconnect{nslog (@ "Server mqtt disconnect!");    Appdelegate *app = (appdelegate *) [[uiapplication sharedapplication] delegate];    Mqttclient *mosq = [app servermqclient]; After disconnecting automatically re-connect//3g or wifi any one reachable on re-connect if ([Common isenable3g] | |    [Common Isenablewifi])   {[Mosq reconnect]; Remote MQTT server End multiplicity Connection NSString  *servertheme = Getmacaddress (); [Mosq Subscribe:servertheme]; Re-subscribing to the subject}}//handles message-(void) didreceivemessage for each topic: (nsstring*) message topic: (nsstring*) Topic{nslog (@ "Serverm      Qtt%@ =%@ ", 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 send a message to the remote MQTT Server external interface * * @param [in] n/A * @param [out] n/A * @return void * @note */+ (void) PublishMessage: (NSString *) message Withtopictype: (NSString *) topictype{appdelegate *app = (appdelegate *) [[UIApplication sharedApp Lication] delegate];    Mqttclient *mosq = [app servermqclient];  [Mosq publishstring:message Totopic:topictype Retain:no]; The retain type of the message is set to no so that it is not retained in the message buffer pool}-(void) dealloc{[super Dealloc];} @end


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.