连接丢失 (32109) - java.io.EOFException (MqttAndroidClient)
Connection lost (32109) - java.io.EOFException (MqttAndroidClient)
P.S。请在得出任何结论之前阅读整个问题。
每当调用 connect 方法时,我都会收到以下异常:
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:77)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
我尝试了几个代码片段,但它们都引发了相同的异常。
方法一
private final String serverUri = "tcp://95.177.135.232:9001";
private void mqqtConnectAndroidClient() {
String clientId = "clientID-" + MqttClient.generateClientId();
mqttAndroidClient = new MqttAndroidClient(getMainActivity(), serverUri, clientId);
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setKeepAliveInterval(60);
mqttConnectOptions.setCleanSession(true);
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
try {
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
subscribeToTopic();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
AppLog.Error("Failed to connect to: " + serverUri);
exception.printStackTrace();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
方法二
private final String serverUri = "tcp://95.177.135.232:9001";
private void methodTwo() {
try {
MqttDefaultFilePersistence mdfp = new MqttDefaultFilePersistence(MQTT_DIR);
mqttClient = new MqttClient(serverUri, "1", mdfp);
MqttConnectOptions options = new MqttConnectOptions();
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
options.setKeepAliveInterval(60);
options.setAutomaticReconnect(true);
options.setCleanSession(true);
mqttClient.connect(options);
mqttClient.subscribe("#");
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable arg0) {
AppLog.Error("Connection lost");
try {
mqttClient.connect();
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
AppLog.Error("deliveryComplete");
}
@Override
public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
AppLog.Error("messageArrived");
AppLog.Error(arg1.toString());
}
});
} catch (MqttException e) {
AppLog.Error("Main Exception");
e.printStackTrace();
}
}
订阅方式
public void subscribeToTopic() {
try {
String subscriptionTopic = "#";
mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
AppLog.Error("Subscribed!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
AppLog.Error("Failed to subscribe");
}
});
} catch (MqttException ex) {
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}
如果我在 HiveMQ 尝试相同的服务器 URI,它在正确连接的情况下工作得很好,订阅也给我结果。我正在使用以下两个库:
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
我尝试了一些 SO 答案,例如 One, , Three, Four and ,但其中 none 对我有用。
如有任何帮助,我们将不胜感激。提前致谢!
您正在使用端口 9001,并且 HiveMQ Link 指向 MQTT over Websockets 客户端。
您正在指定一个以 tcp://
开头的连接 URI,这将向服务发出信号,表明它应该使用本机 MQTT 而非 Websockets 上的 MQTT 进行连接。
如 bug 实现 Websocket 支持的报告中所述,您需要使用 ws://
启动连接 URI,让客户端代码知道使用 Websockets。
我已解决问题,请在此处查看更多详细信息
https://github.com/eclipse/paho.mqtt.java/issues/207
很有趣,但即使在生成唯一客户端 ID 时我们也会看到这一点。多台机器上有多个客户端,所以我猜有可能是多台机器生成 clientID,但对于重复的 clientIds 来说,精确到纳秒级的可能性很小。
P.S。请在得出任何结论之前阅读整个问题。
每当调用 connect 方法时,我都会收到以下异常:
Connection lost (32109) - java.io.EOFException at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146) at java.lang.Thread.run(Thread.java:818) Caused by: java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:77) at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65) at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
我尝试了几个代码片段,但它们都引发了相同的异常。
方法一
private final String serverUri = "tcp://95.177.135.232:9001";
private void mqqtConnectAndroidClient() {
String clientId = "clientID-" + MqttClient.generateClientId();
mqttAndroidClient = new MqttAndroidClient(getMainActivity(), serverUri, clientId);
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setKeepAliveInterval(60);
mqttConnectOptions.setCleanSession(true);
mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
try {
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
subscribeToTopic();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
AppLog.Error("Failed to connect to: " + serverUri);
exception.printStackTrace();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
方法二
private final String serverUri = "tcp://95.177.135.232:9001";
private void methodTwo() {
try {
MqttDefaultFilePersistence mdfp = new MqttDefaultFilePersistence(MQTT_DIR);
mqttClient = new MqttClient(serverUri, "1", mdfp);
MqttConnectOptions options = new MqttConnectOptions();
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
options.setKeepAliveInterval(60);
options.setAutomaticReconnect(true);
options.setCleanSession(true);
mqttClient.connect(options);
mqttClient.subscribe("#");
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable arg0) {
AppLog.Error("Connection lost");
try {
mqttClient.connect();
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
AppLog.Error("deliveryComplete");
}
@Override
public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
AppLog.Error("messageArrived");
AppLog.Error(arg1.toString());
}
});
} catch (MqttException e) {
AppLog.Error("Main Exception");
e.printStackTrace();
}
}
订阅方式
public void subscribeToTopic() {
try {
String subscriptionTopic = "#";
mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
AppLog.Error("Subscribed!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
AppLog.Error("Failed to subscribe");
}
});
} catch (MqttException ex) {
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}
如果我在 HiveMQ 尝试相同的服务器 URI,它在正确连接的情况下工作得很好,订阅也给我结果。我正在使用以下两个库:
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
我尝试了一些 SO 答案,例如 One,
如有任何帮助,我们将不胜感激。提前致谢!
您正在使用端口 9001,并且 HiveMQ Link 指向 MQTT over Websockets 客户端。
您正在指定一个以 tcp://
开头的连接 URI,这将向服务发出信号,表明它应该使用本机 MQTT 而非 Websockets 上的 MQTT 进行连接。
如 bug 实现 Websocket 支持的报告中所述,您需要使用 ws://
启动连接 URI,让客户端代码知道使用 Websockets。
我已解决问题,请在此处查看更多详细信息 https://github.com/eclipse/paho.mqtt.java/issues/207
很有趣,但即使在生成唯一客户端 ID 时我们也会看到这一点。多台机器上有多个客户端,所以我猜有可能是多台机器生成 clientID,但对于重复的 clientIds 来说,精确到纳秒级的可能性很小。