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 单例模式的设置
*
* @param [in] N/A
* @param [out] N/A
* @return void
* @note
*/ +(MqttFramework *)getMQttFrameInstance; /**
* @brief 给mqttserver发送消息 临时未做使用 待扩展
*
* @param [in] N/A
* @param [out] N/A
* @return void
* @note
*/ +(void)publishMessage:(NSString *)message withTopicType:(NSString *)topicType; /**
* @brief 请求的结果处理
*
* @param [in] N/A
* @param [out] N/A
* @return void
* @note
*/ -(void)requestResult:(NSString *)topic withData:(id)resultData; /**
* @brief 定义mqtt消息主题
*
* @param [in] N/A
* @param [out] N/A
* @return void
* @note
*/ -(void)productTheme:(NSString *)theme; /**
* @brief 重连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 得到模块控制器的句柄单例
*
* @param [in] N/A
* @param [out] N/A
* @return void
* @note
*/ static MqttFramework *mqttInstance = nil;
+(MqttFramework*)getMQttFrameInstance
{
@synchronized(self)
{
if(mqttInstance==nil)
{
mqttInstance=[[self alloc]init];
}
}
return mqttInstance;
} #pragma --mark BusinessModuleProtocol delegate
- (int)initBusinessModule:(BusinessModuleInfo*)info
{ info.businessModuleIdEX = @"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]; // [mosq setHost:Mqtt_URL];
// [mosq connect];
// [mosq subscribe:@"nanode/red_led"];
// [mosq subscribe:@"nanode/green_led"];
// [mosq subscribe:@"iCombo"];
// [mosq subscribe:@"iPhone"]; //1.组装xml工作
//2.发送请求消息
//在此正式调用mqtt请求 NSString *sendStr = [[NSString alloc] initWithData:[XmlAdept makeMqttXML:MessageType withDictionary:(NSDictionary *)messageVector] encoding:NSUTF8StringEncoding]; // /****************去掉最后换行符********************/
int index = sendStr.length-1;
sendStr = [sendStr substringToIndex:index]; // /*********************end*************************/ // ASIFormDataRequest *formDataRequest = [ASIFormDataRequest requestWithURL:nil];
//NSString *messageEx = [formDataRequest encodeURL:sendStr];
// NSString *messageEx = [formDataRequest encodeURL:@"我家"];
//[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(@"发送业务广播 给监听者");
[businessFrameworks_ broadcastBusinessNotifyEX:topic withInParam:resultData];
} /**
* @brief 定义mqtt消息主题
*
* @param [in] N/A
* @param [out] N/A
* @return void
* @note
*/ -(void)productTheme:(NSString *)theme
{
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 重连mqttserver
*
* @param [in] N/A
* @param [out] N/A
* @return void
* @note
*/
-(void)reconnectMqtt
{
AppDelegate *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(@"连接本地mosquito代理server返回码为:%d 连接mqtt成功",code);
}
else
{
NSLog(@"连接Mqttserver失败");
} } - (void) didDisconnect
{
NSLog(@"mqtt disconnect!");
} //处理各个主题相应的message - (void) didReceiveMessage: (NSString*)message topic:(NSString*)topic
{
NSLog(@"%@ => %@", topic, message);
//deal with diffrent message here:
//if else 分支处理
//1.xml的解析工作
//将收到的消息处理后然后广播出去 接收者会依据主题处理自己的业务
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后回调的messageId
- (void) didPublish: (NSUInteger)messageId
{
NSLog(@"send messageId = %d",messageId);
} //定制一个主题
- (void) didSubscribe: (NSUInteger)messageId grantedQos:(NSArray*)qos
{
NSLog(@"messageId = %d grantedQos = %@",messageId,qos);
} //取消一个主题
- (void) didUnsubscribe: (NSUInteger)messageId
{
NSLog(@"unsubscibe messageId = %d",messageId);
} /**
* @brief 给mqttserver发送消息 对外接口
*
* @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 mosquittoClient];
[mosq publishString:message toTopic:topicType retain:NO]; //消息的retain类型设置为NO让其不在消息缓冲池中保留
} -(void)dealloc
{
[super dealloc];
} @end

Mqtt协议IOS端移植2的更多相关文章

  1. Mqtt协议IOS端移植3

    ServerMqFramework.h #import "MqttFramework.h" @interface ServerMqFramework : MqttFramework ...

  2. Mqtt协议IOS移植完1

    MQTTClient.h #import <Foundation/Foundation.h> @protocol MQTTDelegate <NSObject> /** * @ ...

  3. 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表

    1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...

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

    前言 MQTT(Message Queuing Telemetry Transport),是一个物联网传输协议,它被设计用于轻量级的发布/订阅式消息传输,旨在为低带宽和不稳定的网络环境中的物联网设备提 ...

  5. mqtt协议实现 java服务端推送功能(一)安装

    最近有个新需求,需要通过java服务端把信息推送到mqtt服务器上,安卓和ios端从mqtt服务器上获取信息实现推送. 1. 本地需要安装Mosquitto服务器  http://mosquitto. ...

  6. MQTT 协议学习:008-在STM32上移植MQTT

    前言 通过前面了解MQTT有关概念.分析了有关的报文,我们对于这个协议也有了更深的认识.但纸上谈来终觉浅,绝知此事要躬行. 本文参考:<STM32+W5500+MQTT+Android实现远程数 ...

  7. mqtt协议实现 java服务端推送功能(三)项目中给多个用户推送功能

    接着上一篇说,上一篇的TOPIC是写死的,然而在实际项目中要给不同用户 也就是不同的topic进行推送 所以要写活 package com.fh.controller.information.push ...

  8. mqtt协议实现 java服务端推送功能(二)java demo测试

    上一篇写了安装mosQuitto和测试,但是用cmd命令很麻烦,有没有一个可视化软件呢? 有,需要在google浏览器下载一个叫MQTTLens的插件 打开MQTTLens后界面如下: 打开conne ...

  9. 基于MQTT协议进行应用开发

    官方协议有句如下的话来形容MQTT的设计思想: "It is designed for connections with remote locations where a "sma ...

随机推荐

  1. java模拟生日发祝福

    1.新建customer表生日都选为当天 所需jar包 2.使用c3p0连接到数据的xml配置文件 3.连接数据库的工具类 package com.cc.birthday; import java.s ...

  2. yum安装jdk环境变量配置

    系统版本 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) #安装之前先查看一下有无系统自带jdk rpm ...

  3. hdu 2544(SPFA)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. Matlab,C++存取二进制

    1,Matlab存储二进制 load Wall.dat %读取数据,数组名为Wall fid=fopen('Wall','wb'); %打开一个文件,二进制写入 fwrite(fid,Wall','f ...

  5. 在C#中将金额转换成中文大写金额

    具体代码如下: /// <summary> /// 金额转换成中文大写金额 /// </summary> /// <param name="LowerMoney ...

  6. SQL表操作习题6 36~45题

  7. 集合框架(01)Collection

    1.集合:存储对象. 对象多了用集合存,数据多了用对象存 2.数组是固定长度,集合是不固定长度:数组是相同数据类型,集合是存储不同类型的对象 3 . 4.为什么会出现这么多的容器那,因为每一个容器对数 ...

  8. xcode 6 exporting ipa 提示 Your account already has a valid iOS distribution certificate 的另一种解决方法

    背景: 1. XCode 6.1 2. 证书:develop 证书 3. Scheme 为Device 操作: 在Product - Archive 包过程中,选择Save for Ad hoc De ...

  9. sharepoint2010新建网站与网站集

    1.以管理员身份运行[sharepoint管理中心] 2.点击创建web应用程序 3.切换用户,我的个人用户不行,需要切换administrator用户 可以看到换成administrator账号 新 ...

  10. 设计模式之过滤器模式(php实现)

    /** * github地址:https://github.com/ZQCard/design_pattern * 过滤器模式(Filter Pattern)或标准模式(Criteria Patter ...