mqtt客户端具有相同的客户端ID。 如何关闭旧连接? 就像这样。
New connection from 192.168.3.57 on port 1883.
1479826181: Client paho166768969170988 already connected, closing old connection.
1479826181: Client paho166768969170988 disconnected.
1479826181: New client connected from 192.168.3.57 as paho166768969170988 (c1, k60, u'admin').
编辑:
String clientId ="**public_cloud**";
try {
MqttClient sampleClient = new MqttClient(config.getBroker(), clientId);
sampleClient.setCallback(new PushCallback());
MqttTopic mtopic = sampleClient.getTopic(config.getTopic());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setConnectionTimeout(100);
connOpts.setKeepAliveInterval(200);
connOpts.setUserName(config.getUsername());
connOpts.setPassword(config.getPassword().toCharArray());
connOpts.setWill(mtopic, "close".getBytes(), 0, true);
sampleClient.connect(connOpts);
} catch (MqttException e) {
e.printStackTrace();
}
}
如果clientid相同,则有错误。
ConnectionLost,cause:(32109) - java.io.EOFException
答案 0 :(得分:0)
String clientId ="**public_cloud**";
try {
MqttClient sampleClient = new MqttClient(config.getBroker(), clientId);
sampleClient.setCallback(new PushCallback());
MqttTopic mtopic = sampleClient.getTopic(config.getTopic());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setConnectionTimeout(100);
connOpts.setKeepAliveInterval(200);
connOpts.setUserName(config.getUsername());
connOpts.setPassword(config.getPassword().toCharArray());
connOpts.setWill(mtopic, "close".getBytes(), 0, true);
sampleClient.connect(connOpts);
} catch (MqttException e) {
e.printStackTrace();
}
}
如果clientId
相同,则会出现错误:
ConnectionLost,cause:(32109) - java.io.EOFException
答案 1 :(得分:0)
与代理的每个连接必须具有唯一的客户端ID。这是MQTT规范的一部分。代理如何使用现有客户端ID处理新连接,但大多数将断开最旧的连接。
您所看到的情况中的常见修复是使用随机生成的客户端ID或当前时间戳
long time = new Date().getTime();
String clientId ="public_cloud" + time;
try {
MqttClient sampleClient = new MqttClient(config.getBroker(), clientId);
sampleClient.setCallback(new PushCallback());
MqttTopic mtopic = sampleClient.getTopic(config.getTopic());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setConnectionTimeout(100);
connOpts.setKeepAliveInterval(200);
connOpts.setUserName(config.getUsername());
connOpts.setPassword(config.getPassword().toCharArray());
connOpts.setWill(mtopic, "close".getBytes(), 0, true);
sampleClient.connect(connOpts);
} catch (MqttException e) {
e.printStackTrace();
}
}