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的更多相关文章
- Mqtt协议IOS端移植2
MqttFramework.h #import <Foundation/Foundation.h> #import "MQTTClient.h" #import &qu ...
- Mqtt协议IOS移植完1
MQTTClient.h #import <Foundation/Foundation.h> @protocol MQTTDelegate <NSObject> /** * @ ...
- 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表
1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...
- MQTT协议学习及实践(Linux服务端,Android客户端的例子)
前言 MQTT(Message Queuing Telemetry Transport),是一个物联网传输协议,它被设计用于轻量级的发布/订阅式消息传输,旨在为低带宽和不稳定的网络环境中的物联网设备提 ...
- mqtt协议实现 java服务端推送功能(一)安装
最近有个新需求,需要通过java服务端把信息推送到mqtt服务器上,安卓和ios端从mqtt服务器上获取信息实现推送. 1. 本地需要安装Mosquitto服务器 http://mosquitto. ...
- MQTT 协议学习:008-在STM32上移植MQTT
前言 通过前面了解MQTT有关概念.分析了有关的报文,我们对于这个协议也有了更深的认识.但纸上谈来终觉浅,绝知此事要躬行. 本文参考:<STM32+W5500+MQTT+Android实现远程数 ...
- mqtt协议实现 java服务端推送功能(三)项目中给多个用户推送功能
接着上一篇说,上一篇的TOPIC是写死的,然而在实际项目中要给不同用户 也就是不同的topic进行推送 所以要写活 package com.fh.controller.information.push ...
- mqtt协议实现 java服务端推送功能(二)java demo测试
上一篇写了安装mosQuitto和测试,但是用cmd命令很麻烦,有没有一个可视化软件呢? 有,需要在google浏览器下载一个叫MQTTLens的插件 打开MQTTLens后界面如下: 打开conne ...
- 基于MQTT协议进行应用开发
官方协议有句如下的话来形容MQTT的设计思想: "It is designed for connections with remote locations where a "sma ...
随机推荐
- PERL学习之模式匹配
一.简介 模式指在字符串中寻找的特定序列的字符,由反斜线包含:/def/即模式def.其用法如结合函数split将字符串用某模式分成多个单词:@array = split(/ /, $line); ...
- Educational Codeforces Round 31- D. Boxes And Balls
D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...
- Spring,Mybatis,Springmvc框架整合项目(第一部分)
一.说在前面的话 本篇博文实现一个注册登录小项目,使用spring,mybatis,springmvc框架进行整合,我们创建的是一个maven工程,主要是方便jar包版本的管理.项目使用eclispe ...
- 00032_ArrayList集合的遍历
1.通过集合遍历,得到集合中每个元素,这是集合中最常见的操作 2.集合的遍历与数组的遍历很像,都是通过索引的方式 public class ArrayListDemo02 { public stati ...
- 工作中的Python脚本
本章内容 1.rawlog处理 2.域名item接口刷新 3.备案结果查询 4.多级域名中取主域 5.发送邮件 6.通过api获取cdn edge ip 7.多线程下载 1.rawlog处理 脚本 ...
- 八、docker的常用命令
1 Docker常用命令 1.1 存储镜像 如果要导出镜像到本地文件,可以使用 docker save 命令. docker save -o log_v140.tar docker.io/vmware ...
- Excel读取导入数据库碰到的问题
1.未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序. 下载并安装驱动:http://download.microsoft.com/download/7/0/3/703 ...
- Python3常用模块的安装
1.mysql驱动:mysql-connector-python 1.安装 $ pip3 install mysql-connector-python --allow-external mysql-c ...
- hdu 4764 巴什博弈
Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- eclipse逆向生成实体类
(转自:http://blog.csdn.net/wangpeng047/article/details/6877720) 做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理 ...