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

Mqtt协议IOS端移植3

时间: 2024-10-08 05:37:56

Mqtt协议IOS端移植3的相关文章

Mqtt协议IOS端移植1

MQTTClient.h #import <Foundation/Foundation.h> @protocol MQTTDelegate <NSObject> /** * @brief 连接服务器 * * @param [in] N/A * @param [out] N/A * @return void * @note */ - (void) didConnect: (NSUInteger)code; /** * @brief 与服务器断开连接 * * @param [in] N

Mqtt协议IOS端移植2

MqttFramework.h #import <Foundation/Foundation.h> #import "MQTTClient.h" #import "BusinessModuleProtocol.h" #import "BusinessFramework.h" #import "AppDelegate.h" @interface MqttFramework : NSObject<MQTTDele

Mqtt协议IOS移植完1

MQTTClient.h #import <Foundation/Foundation.h> @protocol MQTTDelegate <NSObject> /** * @brief 连接server * * @param [in] N/A * @param [out] N/A * @return void * @note */ - (void) didConnect: (NSUInteger)code; /** * @brief 与server断开连接 * * @param

MQTT协议学习及实践(Linux服务端,Android客户端的例子)

前言 MQTT(Message Queuing Telemetry Transport),是一个物联网传输协议,它被设计用于轻量级的发布/订阅式消息传输,旨在为低带宽和不稳定的网络环境中的物联网设备提供可靠的网络服务.MQTT是专门针对物联网开发的轻量级传输协议.MQTT协议针对低带宽网络,低计算能力的设备,做了特殊的优化,使得其能适应各种物联网应用场景.本文旨在研究其在消息发布/订阅/接收场景下的应用. MQTT协议中的几个重要概念 服务端 是发送消息的客户端和请求订阅的客户端之间的中介,又称

深度剖析MQTT协议的整个通信流程

http://www.elecfans.com/d/587483.html MQTT,目前物联网的最主要的协议,基本所有收费的云平台都是基于MQTT协议,比如机智云,和所有的开放云平台比如中国移动的oneNet.百度的云平台也都支持MQTT的接入.虽然MQTT很火,但是目前对MQTT的很少,尤其是在如何移植到嵌入式上来.大部分的平台商的做法都是跟模块商合作把MQTT协议集成到WiFi模块跟GPRS模块里面,捆绑一销售,不让用户过多的了解物联网最核心的东西. 在还没有深入去了解MQTT协议之前,总

MQTT协议及推送服务(二)

MQTT简介 MQTT全称叫做Message Queuing Telemetry Transport,意为消息队列遥测传输,是IBM开发的一个即时通讯协议.由于其维护一个长连接以轻量级低消耗著称,所以常用于移动端消息推送服务开发. MQTT特性 MQTT具有如下特性: 使用发布/订阅消息模式,提供一对多消息发布: 对负载内容屏蔽的消息传输: 使用TCP/IP进行网络连接: 主流的MQTT是基于TCP进行连接的,同样也有UDP版本的MQTT,但是不太常用,叫做MQTT-SN. 具有三种消息发布服务

MQTT协议简记

 一.定义 MQTT - MQ Telemetry Transport 轻量级的 machine-to-machine 通信协议. publish/subscribe模式. 基于TCP/IP. 支持QoS. 适合于低带宽.不可靠连接.嵌入式设备.CPU内存资源紧张. 是一种比较不错的Android消息推送方案. FacebookMessenger采用了MQTT. MQTT有可能成为物联网的重要协议. MQTT是轻量级基于代理的发布/订阅的消息传输协议,它可以通过很少的代码和带宽和远程设备连接.例

iOS端 HTML5的简单了解

/*------------------------------------- 01 HTML5 ---------------------------------------*/ { hosts: 帮助我们记录一些IP地址! 测试服务器地址: itcast } HTML(网页内容!) + CSS(让网页变的好看!) + JS (让网页动起来!(与用户交互!)) 重点:1.了解 HTML5 . { 1. 什么是 HTML5 ? HTML 的全称是:HyperText Markup Languag

转:XMPP协议、MQTT协议、HTTP协议、CoAP协议的基本比较

一.先看下相关国外的专业数据对四大协议的比较: Protocol                                    CoAP                         XMPP                                                  RESTful HTTP                                                                 MQTT Transport UDP TCP