MQTT-客户端框架保持在后台运行 [英] MQTT-Client-Framework stay running at background

查看:699
本文介绍了MQTT-客户端框架保持在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 MQTT-Client-Framework 连接到我们的推送服务器

I'm trying to connect to our push server via MQTT-Client-Framework.

连接服务器没有复杂性,只需几行代码,我就可以连接到服务器并订阅主题.

There is no complication for connecting to server and with a few line of code i can connect to server and subscribe to topic.

但是我有几个问题,我找不到他们的直接答案.

but I have a few question that I could not find any direct answer for them.

1)如何保持我的客户端在后台运行?

1) How can I keep my client running at background?

2)设备重启后会发生什么?设备重启后如何自动连接服务器?

2) What happen after device restart? how can I automatically connect to server after device restart?

3)假设我在连接服务器时遇到错误.该库是否将尝试循环连接?尝试几次?还是我需要自己解决这个问题?

3)Suppose I got an error during connecting to server. Will this library trying to connect in a loop? how many times it try? or I need to manage this myself?

4)订阅主题的3种情况相同?

4) The same 3 scenario for subscribing to topic?

推荐答案

根据我在MQTT客户端框架上的经验,以下是对您的问题/查询的回答.我希望它可以澄清您的疑虑并帮助您前进.

Based on my experience on MQTT-client framework following are the answers to your questions/queries. I hope it clarifies your concerns and helps you to move ahead.

1)如何保持我的客户端在后台运行?

1) How can I keep my client running at background?

  • You can not keep your MQTT client running in background, as Apple doesn't allow any application to keep running for long time in background. Though if you override its not guaranteed your application will keep running in background. You can read more about background execution support in apple documentation.
  • Also refer issue posted on github for given framework.

2)设备重启后会发生什么?设备重启后如何自动连接服务器?

2) What happen after device restart? how can I automatically connect to server after device restart?

  • 每次您的应用开始执行时,您都需要使用MQTT客户端框架连接到服务器,MQTT客户端框架中没有可用的自动连接机制.我建议将连接初始化写入特定的控制器中,该控制器在您的应用启动后立即执行,但与AppDelegate
  • 相同

3)假设我在连接服务器时出错.该库是否将尝试循环连接?尝试几次?还是我需要自己解决这个问题?

3) Suppose I got an error during connecting to server. Will this library trying to connect in a loop? how many times it try? or I need to manage this myself?

  • 如果您的MQTT客户端无法连接服务器,则需要您自己处理它,库不会像上一个答案中所述尝试自动连接.我写了下面的示例代码.使用NSTimer自动连接到服务器.

[self.mqttSession connectToHost:MQTT_HOST port:MQTT_PORT usingSSL:NO connectHandler:^(NSError *error)
{
    if(error)
    {
        // Application fail to connect to server, write your code to auto connect here
    }
}];

4)订阅主题的3种情况相同?

4) The same 3 scenario for subscribing to topic?

  • 如果您的代理服务器已配置为跟踪单个用户/客户端的现有订阅,则您无需每次都订阅.
  • 否则,每次您需要订阅成功连接的同一主题时,就可以这样做.使用以下MQTTSessionDelegate方法进行订阅.

- (void)connected:(MQTTSession *)session

快乐编码:)

这篇关于MQTT-客户端框架保持在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆