丢失连接(32109) - java.io.EOFException(MqttAndroidClient)
P.S.请在结束任何事情之前阅读整个问题。丢失连接(32109) - java.io.EOFException(MqttAndroidClient)
我得到以下异常时连接方法被称为:
连接丢失(32109) - java.io.EOFException的 在org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run (CommsReceiver.java:146) at java.lang.Thread.run(Thread.java:818) 由于: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)
我已经尝试了几个代码片段,但他们都提出了相同的异常。
方法1
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();
}
}
方法2
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();
}
}
如果我尝试在同一台服务器在URI HiveMQ,通过适当的连接完成正常工作,订阅也给我结果。我使用了以下两个库:
编译 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' 编译“org.eclipse.paho:org.eclipse.paho .android.service:1.0.2'
我尝试了一些SO答案喜欢One,Two,Three,Four和Five但他们都不是为我工作。
任何帮助将不胜感激。先谢谢你!
您正在使用端口9001,并且HiveMQ链接指向通过Websockets客户端的MQTT。
您正在指定一个以tcp://
开头的连接URI,它将通知服务它应该通过Websockets与本机MQTT而非MQTT连接。
如实施Websocket支持的bug报告中所述,您需要使用ws://
开始连接URI以让客户端代码知道使用Websocket。
是的你是对的。我尝试过ws://但是使用web socket客户端,在这种情况下它拒绝了。 –