丢失连接(32109) - java.io.EOFException
我使用eclipse MQTT连接到MQTT服务器。丢失连接(32109) - java.io.EOFException
我可以连接到服务器成功,但是当我发布消息,我得到这个错误
连接丢失
消息:连接丢失
LOC:连接丢失 原因:java.io.EOFException的
excep:连接丢失(32109) - java.io.EOFException
我搜索了这个问题。但我找不到任何真正的答案!一些链接我公司成立于此处{here,here,here,...}
我的代码:
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 : [email protected]
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
的Javadoc connectWithResult建议调用setCallback(MqttCallback)
,以便发往所述客户端的消息可以只要客户端连接接受连接之前进行。
尝试在您的源代码中调用mqttClient.setCallback
。
这不是真实的答案。我这样做。但问题没有解决 – b4hr4m
运行您的程序。如果这可以帮助任何人...... 我有同样的异常,并通过确保一个唯一的客户端ID固定它生成(与MqttAsyncClient.generateClientId()
),这里提到: https://github.com/eclipse/paho.mqtt.java/issues/207#issuecomment-338246879
我这样做。但是这个错误没有解决 – b4hr4m
您是否有权访问代理中的日志以了解为什么它关闭了连接?我的第一个猜测是,您无权发布您正在使用的主题。 – hardillb
@hardillb不,我不检查日志。你能告诉我日志在哪里吗?我该如何检查? – b4hr4m
不知道更多的信息,例如您使用的代理商以及安装方式。 (编辑问题以添加这些详细信息) – hardillb