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的更多相关文章

  1. Mqtt协议IOS端移植2

    MqttFramework.h #import <Foundation/Foundation.h> #import "MQTTClient.h" #import &qu ...

  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. 如何用纯 CSS 创作背景色块变换的按钮特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/XYKdwg 可交互视频教 ...

  2. axure笔记--内置变量

    部件变量: This:当前变量名称 Target:目标变量的名称 x,y表示组件左上角的位置 name:获取当前组件标签命名 Top:获取组件上边界到x轴的距离 bottom:获取组件下边界到x轴的距 ...

  3. Python运算符总结

    一.简介 Python中运算符分为: 算术运算符:主要用于两个对象算数计算(加减乘除等运算) 比较(关系)运算符:用于两个对象比较(判断是否相等.大于等运算) 赋值运算符:用于对象的赋值,将运算符右边 ...

  4. FSHC之MCU接口部分理解

    |_____________|       |_____|                                                                    |__ ...

  5. 组队赛Day1第一场 GYM 101350 G - Snake Rana (容斥)

    [题意] 给一个N×M的矩阵, K个地雷的坐标.求不含地雷的所有矩形的总数. T组数据. N M都是1e4,地雷数 K ≤ 20 Input 3 2 2 1 2 2 6 6 2 5 2 2 5 100 ...

  6. js--进阶知识点

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【21】外边距折叠(collapsing margins)

    [21]外边距折叠(collapsing margins) 外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距. 合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者. [注意]m ...

  8. 获取class

    使用原生JavaScript,获取类操作符时:即使使用getElementByClassName,在Firefox和IE9以下是不兼容的.Firefox下是可以用它获取的到元素而IE不行,一般框架都会 ...

  9. 【编程工具】Sublime Text3快捷键配置

    我们在使用编译软件时,总是喜欢使用快捷键来方便我们的操作,但有些编译软件不支持快捷键的修改和设置,为了能够更加方便的使用 Sublime Text3,这里我介绍几个 Sublime Text3 设置快 ...

  10. POJ 2643 Election

    Election Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3558   Accepted: 1692 Descript ...