MQTT-Client-Framework stay running at background

2020-06-04 05:12发布

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) How can I keep my client running at background?

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

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) The same 3 scenario for subscribing to topic?

3条回答
女痞
2楼-- · 2020-06-04 06:01

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) 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) What happen after device restart? how can I automatically connect to server after device restart?

  • Each time your app begin execution you need to connect to your server using MQTT client framework there is no auto connect mechanism available in MQTT-client framework. I suggest to write init your connection in specific controller which executes immediately after your app launch except same as AppDelegate

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?

  • If your MQTT-client fails to connect your server, you need to handle it yourself, library doesn't try to auto connect as mentioned in previous answer. I have written sample code as below. Use NSTimer for auto connect to server.

    [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) The same 3 scenario for subscribing to topic?

  • If your broker server has configuration to track your existing subscription for individual users/client then you don't need to subscribe each time.
  • Otherwise each time you need to subscribe to same topic on successful connection. Use following MQTTSessionDelegate method to subscribe.

    - (void)connected:(MQTTSession *)session
    

Happy coding :)

查看更多
Anthone
3楼-- · 2020-06-04 06:04

For your first question:

Details on how to run in the background on iOS can be found here. This link also lists the actions that Apple allows to run in the background, if your app does not meet those criteria then it is likely to get thrown out of the app store by Apple.

The list also shows which UIBackgroundModes to place in your Info.plist to flag that your app needs background access.

The other 3 I can't help with

查看更多
迷人小祖宗
4楼-- · 2020-06-04 06:13

1)Project->Capabilities->Background Modes. There has some options for allowing your app to run at background.

2)Generally speaking, MQTT will not be disconnected to the server if your app is allowed to run at background, but i think you would better check up the connection and maybe re-connect MQTT to your server when the app become active again.

AppDelegate-> - (void)applicationDidBecomeActive:(UIApplication *)application;

3)Unfortunately, yes, it will. And you have to manage yourself.

4)I can't help.

查看更多
登录 后发表回答