本文介绍了如何使用Eclipse泛美卫生组织MQTT客户机发送Ping?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚开始建立我们自己的推送通知系统(因客户的要求)Android和发现Eclipse的泛美卫生组织(的http:// www.eclipse.org/paho/ )。不用说,这个项目是非常令人兴奋。

与Android的问题是,如果CPU处于睡眠状态时,MQTT客户机可能无法获得在其设定的时间间隔发送Ping的机会。解决方法是使用AlarmManager唤醒它,并把工作做好。 Android的文件说:

http://developer.android.com/reference/android/app/AlarmManager.html

我要肯定我能说的onReceive()方法,而CPU有PARTIAL_WAKE_LOCK中发送ping命令,所以我在寻找一种方式来手动发送pi​​ng到服务器,但它似乎在客户端不公开任何这样的方法。我失去了一些东西?或者说,有什么解决办法这里除了出版自己的ping消息?我想避免这种情况的原因为:

  1. 在开销较大
  2. 我们将确保Android的客户端用户仅可与Mosquitto的ACL。他们不会被允许发布消息。
解决方案

我一直在做与MQTT Android上的一些工作,我已经经历了完全一样的问题。

由于戴尔说,旧版本中使用MQTT客户有一个明确的平()方法,但不幸的是,这是现在隐藏起来。

最简单的方法,而我使用的一个,是明确发布1字节的消息给一个特定的主题,以服务为存活。我不认为这应该增加太多的应用程序的开销,虽然我不熟悉Mosquitto的ACL,我认为你可以有每个客户端使用相同的'存活'的话题,只是提供写访问所有。这应该不会影响安全性,只要没有人可以从主题阅读。

这是另一种方法是让服务器(通过一个主题发布/订阅一切为了效率)发送客户端(S),一个保活的消息在QoS的1或2,由于QoS流这一点,将涉及客户机发送一个消息返回到下盖的服务器;这将作为存活。这具有保持你的客户,因为只有用户的优势;然而,这是不兼容的干净届=假'(因为你将有大量的邮件排队交付给客户谁是脱机一段时间 - 不必要的影响再连接上的性能)。

不幸的是,这些是仅有的两种解决方法,我目前可以想到的。


此外,作为一个简短的一边,我已经经历了多次的使用在Android上MqttDefaultFilePersistence问题,所以你可能想知道这一点。特别是与文件锁定和问题时,重新实例化客户端。为了解决这个问题我已经创建MqttClientPersistence的建立在SQLite数据库之上的实现,这是更强大;你可能想这样做。

We've just started building our own push notification system (due to client's requirement) for Android and found Eclipse Paho (http://www.eclipse.org/paho/). Needless to say, this project is really exciting.

The problem with Android is, if the CPU is in sleep state, the MQTT client may not get the chance to send ping at its set interval. The workaround is using AlarmManager to wake it up and get the job done. The Android documentation says:

http://developer.android.com/reference/android/app/AlarmManager.html

I need to be sure that I could send the ping command within that onReceive() method while the CPU has PARTIAL_WAKE_LOCK, so I was searching a way to manually sending ping to server but it seems the client doesn't expose any such method. Am I missing something? Or, what is the workaround here except publishing my own "ping message"? I want to avoid that because of:

  1. Larger overhead
  2. We'll ensure that Android clients are subscriber only, may be with Mosquitto's ACL. They will not be allowed to publish messages.
解决方案

I've been doing some work with MQTT on Android and I've experienced exactly the same issue.

As Dale says, the old version of the MQTT client used to have an explicit ping() method, but unfortunately this is now hidden away.

The simplest approach, and the one I use, is to explicitly publish a 1 byte message to a particular topic, to serve as the keepalive. I don't think this should add much to the overhead of your application and, while I'm not familiar with Mosquitto's ACL, I assume you could have every client use the same 'keepalive' topic and just provide write access to all. This shouldn't affect security as long as no-one can read from the topic.

An alternative approach would be to have the server send the client(s) a 'keepalive' message at QoS 1 or 2 (pub/sub through a single topic to all for efficiency) as, due to the QoS flows, this will involve the client sending a message back to the server under the covers; which will serve as the keepalive. This has the advantage of keeping your clients as subscriber only; however it's incompatible with 'clean session = false' (as you would have large amounts of messages queued up for delivery to clients who are offline for a while - needlessly affecting performance on reconnect).

Unfortunately these are the only two workarounds that I can currently think of.


Also, as a brief aside, I've experienced a number of issues using the MqttDefaultFilePersistence on Android, so you might want to be aware of this. In particular to do with file locking and problems when re-instantiating the client. To get around this I've created an implementation of MqttClientPersistence built on top of an SQLite database and this is much more robust; you might want to do the same.

这篇关于如何使用Eclipse泛美卫生组织MQTT客户机发送Ping?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 13:15