连接丢失 (32109) - java.io.EOFException

问题描述 投票:0回答:5

我使用 eclipse MQTT 连接到 MQTT 服务器。

我可以成功连接到服务器,但是当我发布消息时,我收到此错误

连接丢失
消息:连接丢失
loc:连接丢失 原因:java.io.EOFException
excep:连接丢失(32109)-java.io.EOFException

我搜索过这个问题。但我找不到任何真正的答案!我在这里创建的一些链接{这里这里这里,...}

我的代码:

private final String DEFAULT_HOST = "edge-mqtt.facebook.com";
private final int DEFAULT_PORT = 443;

public void connect(String protogle) throws Exception {

    this.broker =  protogle + "://"+ DEFAULT_HOST + ":" + DEFAULT_PORT;
    this.mqttClient = new MqttClient(broker,getMqttClientId() ,new MemoryPersistence() );

    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval( MQTT_KEEPALIVE);
    connOpts.setUserName( getMqttUsername() );
    connOpts.setPassword( getMqttPassword().toCharArray() );
    //connOpts.setMqttVersion( 3 );//some times it have an error
    //connOpts.setSocketFactory(SSLTunnelSocketFactory.getInstance());
    Logger.w("Connecting to broker: "+broker);
    Logger.w("isConnected:"+mqttClient.isConnected());
    try {
        IMqttToken cn = mqttClient.connectWithResult(connOpts);
        Logger.w("connected");
    }catch (MqttException me){
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
        System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
        return;
    }



    this.mqttClient.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable me) {
            Logger.w("Connection lost");
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
        }

        @Override
        public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
            Logger.w("message Arrived");
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
            Logger.w("deliverd--------");
            try {
                MqttDeliveryToken token  = (MqttDeliveryToken) iMqttDeliveryToken;
                String h = token.getMessage().toString();
                Logger.w("deliverd message :"+h);
            } catch (MqttException me) {
                System.out.println("reason "+me.getReasonCode());
                System.out.println("msg "+me.getMessage());
                System.out.println("loc "+me.getLocalizedMessage());
                System.out.println("cause "+me.getCause());
                System.out.println("excep "+me);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });



}

以及发布方法:

private void publish(String topic , String payload , int qosLevel) throws Exception {
    Logger.w("start publishing :");
    //payload = Helper.zlib_encode(payload);
    topic = mapTopic(topic);

    MqttMessage message = new MqttMessage();
    message.setPayload(payload.getBytes("UTF-8") );
    message.setQos(0);

    mqttClient.publish(topic , message);
    Logger.w("publised------------------");
}

输出:

 Connecting to broker: ssl://edge-mqtt.facebook.com:443  
 isConnected:false  
 connected  
 start publishing :  
 deliverd--------  
 publised------------------  
 deliverd message : //my message
 Connection lost
 msg : Connection lost
 loc : Connection lost
 cause : java.io.EOFException
 excep : Connection lost (32109) - java.io.EOFException

Eclipse 泛美卫生组织日志:

 ============== Connection options ==============
 CleanSession                :  true
 SocketFactory               :  sun.security.ssl.SSLSocketFactoryImpl@6c010ee9
 MqttVersion                 :  3
 KeepAliveInterval           :  60
 ConTimeout                  :  30
 UserName                    :  . . . 
 SSLProperties               :  null
 WillDestination             :  null
 ==========================================
 2017-10-19 09:42:02,182 INFO  [MQTT Call: Bahram091547759    ] [MqttConnectionResultHandler   ]  - insta connected
 2017-10-19 09:42:02,187 INFO  [JavaFX Application Thread     ] [MqttEventHandler              ]  - About to resubscribe to all requested topics
 2017-10-19 09:42:08,559 INFO  [JavaFX Application Thread     ] [MqttAsyncConnection           ]  - Publishing message on topic "k.,".  Payload size = "3"
 2017-10-19 09:42:08,739 ERROR [MQTT Rec: Bahram091547759     ] [MqttCallbackHandler           ]  - Connection insta lost
 Connection lost (32109) - java.io.EOFException
     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java: 146)
     at java.lang.Thread.run(Unknown Source)
 Caused by: java.io.EOFException
     at java.io.DataInputStream.readByte(Unknown Source)
     at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMess age(MqttInputStream.java:65)
     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
     ... 1 more
java mqtt paho
5个回答
21
投票

我遇到了同样的异常,并通过确保生成唯一的客户端 ID(使用

MqttAsyncClient.generateClientId()
)来修复它,如下所述: https://github.com/eclipse/paho.mqtt.java/issues/207#issuecomment-338246879


1
投票

connectWithResult的javadoc建议在连接之前调用

setCallback(MqttCallback)
,以便在客户端连接后立即接受发往客户端的消息。

尝试在源代码中移动

mqttClient.setCallback
调用。

还可以尝试使用 java -Djavax.net.debug=all

运行程序

0
投票

尝试将保持活动时间减少到 15 秒并将ConnectionTimeout 设置为 30

  connOpts.setKeepAliveInterval(15);
  connOpts.setConnectionTimeout(30);

0
投票

最近我正在研究这个。我遇到了同样的问题:成功连接后出现 32109 错误。

这是泛美卫生组织存储库上报告的问题

我更新了我的图书馆:

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.1'

至:

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.2'

正如存储库所说。

这样就解决了问题。


0
投票

就我而言,它与

keepAliveInterval
中的
MqttConnectOptions()
有某种关系。

我将 keepAliveInterval 保持为 10 秒,如果我的应用程序在 10 秒内没有

publishing
任何内容,那么我就会得到这个
EOFException
。 我把它增加到 100 秒,效果很好。

希望这有助于进一步解决问题

© www.soinside.com 2019 - 2024. All rights reserved.