MQTT-客户端框架保持在后台运行 [英] MQTT-Client-Framework stay running at background
问题描述
我正在尝试通过 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?
- 您不能使MQTT客户端在后台运行,因为Apple不允许任何应用程序在后台长时间运行.虽然如果您覆盖它并不能保证您的应用程序将继续在后台运行.您可以在 Apple文档中了解有关后台执行支持的更多信息.
- 还请参阅github上发布的问题以获取给定的框架.
- 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屋!