Paho

来源:互联网 发布:网络推广公司招聘 编辑:程序博客网 时间:2024/05/16 19:27

查了一下LOG,我们是从2014年12月开始用MQTT的,当时找了IBM的MQTT(android)的库,是能正常工作,后来发现一些一个问题,“手机屏幕锁屏后手机不再发PING到服务器”,然后就修复,网上很多是直接用系统的AlarmClock定时加心跳,我们也是这样做的,后来还有小米的问题,也是很困扰,也修正了。

后来测试报一个错,断网后有的机器直接没有重连,查了一轮发现IBM.MQTT的LostConnect(),isConnect(),ping(),几个函数都挂掉了。准备重新找开源库,后来就发现这个:


http://wiki.eclipse.org/Paho


着手实践下


换库之后,在原来的基础上做一点修改就行:

1、

tcp://127.0.0.1@1883

改为

tcp://127.0.0.1:1883

2、

new MqttClient(MqttURL, iHardwareId,new MemoryPersistence());

其他函数看着替换就行。


之前我们在心跳重连方面下了不少功夫,现在看到这个库里的实现,真是有点意外:


public class TimerPingSender implements MqttPingSender {private static final String CLASS_NAME = TimerPingSender.class.getName();private static final  Logger log =                 LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT,CLASS_NAME);private ClientComms comms;private Timer timer;public void init(ClientComms comms) {if (comms == null) {throw new IllegalArgumentException("ClientComms cannot be null.");}this.comms = comms;}public void start() {final String methodName = "start";String clientid = comms.getClient().getClientId();//@Trace 659=start timer for client:{0}log.fine(CLASS_NAME, methodName, "659", new Object[]{clientid});timer = new Timer("MQTT Ping: " + clientid);//Check ping after first keep alive interval.timer.schedule(new PingTask(), comms.getKeepAlive());}public void stop() {final String methodName = "stop";//@Trace 661=stoplog.fine(CLASS_NAME, methodName, "661", null);if(timer != null){timer.cancel();}}public void schedule(long delayInMilliseconds) {timer.schedule(new PingTask(), delayInMilliseconds);}private class PingTask extends TimerTask {private static final String methodName = "PingTask.run";public void run() {//@Trace 660=Check schedule at {0}log.fine(CLASS_NAME, methodName,                            "660", new Object[]{new Long(System.currentTimeMillis())});comms.checkForActivity();}}}











0 0
原创粉丝点击