Android - Paho Mqtt客户端在网络连接更改后不会收到消息(移动数据已禁用并再次启用) [英] Android - Paho Mqtt client does not receive messages once network connectivity changes (mobile data disabled and enabled again)

查看:1755
本文介绍了Android - Paho Mqtt客户端在网络连接更改后不会收到消息(移动数据已禁用并再次启用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mosquitto Mqtt和paho API在Android设备上接收推送消息。但是一旦网络连接发生变化,它就会停止接收消息。以下是使用简单测试用例重现问题的步骤:

I am using Mosquitto Mqtt along with paho API to receive push messages on android device. But as soon as the network connectivity changes it stops receiving messages. Here are the steps to reproduce the issue with simple test case:

1)创建一个简单的活动。

1) Create a simple activity.

2)On Activity StartUp通过paho API连接到mosquitto测试服务器(test.mosquitto.org:1883)。

2) On Activity StartUp connects to mosquitto test server (test.mosquitto.org:1883) through paho API.

3)订阅一些主题。

4)向主题发布一些消息。

4) Publish some message to the topic.

结果: Mqtt Client收到发布的所有消息这个话题。现在

Result: Mqtt Client receives all the messages published to the topic. Now

5)在移动设备上禁用互联网连接(移动数据)

5) Disable internet connection on the mobile (mobile data)

6)向网上发布一些消息主题。

6) Publish some message to the topic.

7)重新连接互联网。

7) Reconnect internet.

结果:客户端无法接收禁用互联网连接后发布的任何消息。

Result: Client Does not receive any message published after internet connection was disabled.

由于 KeepAliveInterval 一直保持高值(30分钟),它应该收到所有消息后重新连接到互联网。

As KeepAliveInterval has been kept to high value (30 minutes), it should receive all messages after reconnecting to the internet.

相同的用例(相同的代码)适用于简单的java项目(非android),我在笔记本电脑上禁用互联网运行用例。

知道为什么它不能在Android设备上运行???我错过了什么吗?

Any idea why it is not working on android device??? Am I missing something?

注意:

1)使用mqtt-client -0.4.1

1) Using mqtt-client-0.4.1

2)Android目标API等级11

2) Android target API level 11

3)在此期间不将设备置于睡眠模式测试。

3) Not putting device to sleep mode during the test.

4)在connectionLost回调中没有得到任何调用,并且mqtt回调的所有4个线程都在整个测试用例中运行,即mosquitto服务器的连接完好无损。

4) Does not get any call in connectionLost callback and all the 4 threads of mqtt callback are running throughout the test case i.e connection the the mosquitto server is intact.

推荐答案

Java客户端库在某种程度上受底层网络API的支配。调用publish时,它会将MQTT数据包写入套接字。如果该写入失败,则将调用连接丢失,如果该写入有效,则客户端库将继续。您看到的行为差异是因为网络库在这些情况下表现不同。

The Java client library is at the mercy of the underlying networking API to a certain extent. When publish is called, it will write an MQTT packet to the socket. If that write fails, then connection lost will be called, if that write works then the client library will carry on. The difference in behaviour you are seeing is because the networking libraries are behaving differently in these circumstances.

MQTT keepalive间隔旨在帮助解决这个问题。在某些情况下,TCP连接可能看起来是实时的。这在移动或卫星连接设备上尤其可行 - 您不能指望网络API在所有情况下都能完全相同。 Keepalive将ping数据包发送到服务器并期望响应 - 如果未收到响应,则假定会话已关闭。

The MQTT keepalive interval is meant to help with this. Under certain circumstances a TCP connection may appear to be live when it is not. This is especially possible on mobile or satellite connected devices - you can't expect the networking APIs to work exactly the same in all circumstances. Keepalive sends a ping packet to the server and expects a response - if that response is not received, the session is assumed to be closed.

如果将keepalive间隔设置为比如说10秒,那么连接应该在15到20秒内被识别为已损坏。

If you set the keepalive interval to say 10 seconds, then the connection should be recognised as broken within 15 to 20 seconds.

这篇关于Android - Paho Mqtt客户端在网络连接更改后不会收到消息(移动数据已禁用并再次启用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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