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 ...
随机推荐
- sed快速学习
- tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04
http://blog.csdn.net/liudijiang/article/details/50555429(转) 首先得要有制作好的uboot和linux内核镜像zImage,先烧录到sd卡里, ...
- Hi3519V101 SDK安装以及开发环境搭建
Hi3519V101 Linux开发环境 1.安装Hi3519V101 SDKHi3519V101 SDK是基于Hi3519V101 DMEB的软件开发包,包含了在Linux相关应用开发时使用的各种工 ...
- 一个线程中lock用法的经典实例
/* 该实例是一个线程中lock用法的经典实例,使得到的balance不会为负数 同时初始化十个线程,启动十个,但由于加锁,能够启动调用WithDraw方法的可能只能是其中几个 作者:http://h ...
- 大数据学习——sqoop导入数据
把数据从关系型数据库导入到hadoop 启动sqoop 导入表表数据到HDFS 下面的命令用于从MySQL数据库服务器中的emp表导入HDFS. sqoop import \ --connect jd ...
- POJ 2157 Maze
Maze Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3183 Accepted: 996 Description A ...
- tab栏切换效 简易效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 对拍程序(Win)
代码如下: @echo off :again rand.exe echo "rand finish" asd.exe echo "1.exe finish" 未 ...
- [UOJ#220][BZOJ4651][Noi2016]网格
[UOJ#220][BZOJ4651][Noi2016]网格 试题描述 跳蚤国王和蛐蛐国王在玩一个游戏. 他们在一个 n 行 m 列的网格上排兵布阵.其中的 c 个格子中 (0≤c≤nm),每个格子有 ...
- 2013 Asia acm Hangzhou Regional Contest 杭州现场赛
B Stealing Harry Potter's Precious 题目大意:给定一个n*m的地图,某些点可以走,某些点可以走某些点不可以走,给定一个起点,又给出了k个点k<=4,要求从起点 ...