MQTT protocol iOS Port porting 2

Source: Internet
Author: User

MqttFramework.h

#import <Foundation/Foundation.h> #import "MQTTClient.h" #import "BusinessModuleProtocol.h" #import " BusinessFramework.h "#import" AppDelegate.h "@interface mqttframework:nsobject<mqttdelegate, businessmoduleprotocol>{businessframework *businessframeworks_;} /** * @brief The settings of the singleton mode * * @param [in] n/A * @param [out] n/A * @return void * @note */+ (mqttframework *) Getmqttframeinstanc e;/** * @brief send a message to mqttserver temporarily unused to be extended * * @param [in] n/A * @param [out] n/A * @return void * @note */+ (void) Publish Message: (NSString *) message Withtopictype: (NSString *) topictype;/** * @brief The result of the request processing * * @param [in] n/A * @param [out] N/A * @return void * @note */-(void) Requestresult: (NSString *) topic Withdata: (ID) resultdata;/** * @brief define MQTT Message Subject * * @  param [in] n/A * @param [out] n/A * @return void * @note */-(void) Producttheme: (NSString *) theme;/** * @brief re-connect Mqttserver * * @param [in] n/A * @param [out] n/A * @return void * @note */-(void) reconnectmqtt; @end


Mqttframework.m


#import "MqttFramework.h" #import "MQTTClient.h" #import "XmlAdept.h" #import "ASIFormDataRequest.h" @implementation mqttframework/** * @brief Get the handle of the module controller single example * @param [in] n/A * @param [out] n/A * @return void * @note */static Mqttframewo RK *mqttinstance = nil;+ (mqttframework*) getmqttframeinstance{@synchronized (self) {if (Mqttinstance==nil) {        Mqttinstance=[[self Alloc]init]; }}return mqttinstance;} #pragma--mark businessmoduleprotocol delegate-(int) Initbusinessmodule: (businessmoduleinfo*) info{info.business    Moduleidex = @ "mqttframework";//Businessframeworks_ = Info.businessframework; return 0;} -(int) Callbusinessprocessex: (NSString *) Capabilityid withmessagetype: (NSString *) MessageType withmessage: (ID)        messagevector{NSLog (@ "Capabilityid =%@ Topic =%@ Messagevector =%@", capabilityid,messagetype,messagevector); Appdelegate *app = (appdelegate *) [[uiapplication sharedapplication] delegate];        Mqttclient *mosq = [app mosquittoclient]; [Mosqsethost:mqtt_url];//[Mosq connect];//[mosq subscribe:@ "nanode/red_led"];//[mosq subscribe:@ "nanode/green_led"];/        /[Mosq subscribe:@ "Icombo"];//[mosq subscribe:@ "IPhone"]; 1. Assemble XML work//2. Send a request message///This formally calls the MQTT request NSString *sendstr = [[NSString alloc] initwithdata:[xmladept makemqttx    Ml:messagetype withdictionary: (Nsdictionary *) messagevector] encoding:nsutf8stringencoding];    /**************** remove last line break ********************/int index = sendstr.length-1;    SENDSTR = [Sendstr substringtoindex:index];  /*********************end*************************///Asiformdatarequest *formdatarequest = [ASIFormDataRequest    Requestwithurl:nil];     NSString *messageex = [Formdatarequest encodeurl:sendstr];//nsstring *messageex = [Formdatarequest encodeURL:@ "my Home"];    [Mosq publishstring:sendstr totopic:@ "Icombo" retain:yes];//NSLog (@ "%@", Messageex);    NSString *string = [NSString stringwithformat:@ "**%@**", sendstr]; NSLog (@ "Sendermesg =%@ ", string);        [Mosq publishstring:sendstr totopic:@ "Icombo" retain:no]; return 0;}    -(void) Requestresult: (NSString *) topic Withdata: (ID) resultdata{NSLog (@ "send 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{appdelegate *app = (appdelegate *) [[uiapplication sharedapplication] delegate];    Mqttclient *mosq = [app mosquittoclient];    Nsuserdefaults *ud = [Nsuserdefaults standarduserdefaults];    if ([[UD objectforkey:@ "host"] isequaltostring:@ "no"]) {[Mosq sethost:mqtt_url];    } else {[Mosq sethost:[ud stringforkey:@ "host"];    }//[mosq Sethost:mqtt_url];    [Mosq connect]; [Mosq Subscribe:theme];} /** * @brief re-connect mqttserver * * @param [in] n/A * @param [out] n/A * @return void * @note */-(void) reconnectmqtt{Appdele Gate *app = (appdelegate *) [[uiapplication Sharedapplication] delegate];    Mqttclient *mosq = [app mosquittoclient];    Nsuserdefaults *ud = [Nsuserdefaults standarduserdefaults];    if ([UD stringforkey:@ "host"]) {[Mosq sethost:[ud stringforkey:@ ' host '];    } else {[mosq sethost:mqtt_url];   } [Mosq reconnect]; } #pragma--mark mosquittoclientdelegate-(void) Didconnect: (Nsuinteger) code{if (code = = 0) {NSLog (@ "Connect local    Mosquito Proxy server return code:%d connection to mqtt successful ", code);    } else {NSLog (@ "Connection Mqttserver failed"); }}-(void) Diddisconnect{nslog (@ "Mqtt disconnect!");} Handle the corresponding message-(void) didreceivemessage for each topic: (nsstring*) message topic: (nsstring*) Topic{nslog (@ "%@ =%@", topic,    message);   Deal with diffrent message here://if Else Branch handles parsing of the//1.xml//the received message is processed and then broadcast out the recipient will process their business according to the subject Nsarray    *resultarr = [[Nsarray alloc]initwithobjects:message, Nil];    Nsarray *list = [xmladept Mqttparsemessagenode:resultarr]; NSString *type = [[List objectatindex:0] objectforkey:@ "type"];        [Self requestresult:type withdata:list]; }//publish callback after messageid-(void) Didpublish: (Nsuinteger) messageid{NSLog (@ "Send messageId =%d", messageId);} Customizing a Theme-(void) Didsubscribe: (Nsuinteger) messageId Grantedqos: (nsarray*) qos{NSLog (@ "messageId =%d Grantedqos =%@", Messageid,qos);} Cancel a topic-(void) Didunsubscribe: (Nsuinteger) messageid{NSLog (@ "Unsubscibe messageId =%d", messageId);} /** * @brief Send message to Mqttserver 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 mosquittoclient];  [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



MQTT protocol iOS Port porting 2

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.