Android Paho + CloudMQTT EOFException 一连接

Android Paho + CloudMQTT EOFException as soon as I connect

在这里完全不知所措,但是当我尝试连接到 CloudMQTT 时,我的客户端崩溃了。

3-05 16:22:54.541 9063-9078/my.pkg.name D/MQTTCONN: About to connect

03-05 16:22:55.341 9063-9091/my.pkg.name D/AlarmPingSender: Unregister alarmreceiver to MqttServiceourTestMqttSubId

03-05 16:22:55.375 9063-9063/my.pkg.name E/MQTTFAIL: Connection lost

03-05 16:22:55.376 9063-9063/my.pkg.name W/System.err: at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)

03-05 16:22:55.377 9063-9063/my.pkg.name W/System.err: at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)

03-05 16:22:55.377 9063-9063/my.pkg.name W/System.err: at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)

这是我的代码:

final MqttAndroidClient mqttClient = new MqttAndroidClient(
    argsService.getContext(),
    (String) configService.getArg("mqttBroker"),
    activeSubId.getSubId(),
    new MemoryPersistence()
);

MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName((String) configService.getArg("mqttUser"));
options.setPassword(((String) configService.getArg("mqttPassword")).toCharArray());
options.setConnectionTimeout(9999);

Log.d("MQTTCONN", "About to connect");
mqttClient.connect(options, null, new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {
        Log.d("MQTTCONN", "Connect ready");
        mqttClient.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable cause) {
                // Someday, in case we want to recover our service
            }

            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                receiveMessageIntent.enact(
                    new JSONObject(new String(message.getPayload())),
                    pubsub
                );
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {

            }
        });

        try {
            mqttClient.subscribe(getSubId().getSubId(), QOS);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
        Log.e("MQTTFAIL", exception.getMessage());
        exception.printStackTrace();
    }
});

我已经尝试过的故障排除:

致将来可能偶然发现此问题的人 - MQTT 是一个相当简单、无忧的协议设置。像这样的问题最有可能出现在服务器端。

就我而言,很可能是 CloudMQTT 的免费套餐 connection/usage 限制 导致了此问题。在花了很多时间试图弄清楚 Paho 的内部结构之后,我简单地使用一个方便的 Ansible 角色和 AWS 启动了我自己的 Mosquitto 实例,并且事情像几个小时前应该发生的那样膨胀:-)

我没有将 CloudMQTT 用作代理,但我在使用其他代理 (RabbitMQ) 时遇到了问题。事实证明,在更改持久 (qos1+) 队列的 TTL 后,我不得不删除所有现有队列并让客户端重新创建它们。大概更改 clientid 也会起作用。