如何实现ping请求?我按照这个代码。我阅读了以下链接How to send ping using Eclipse Paho MQTT client?。但是我无法实现它。
package mqttTestJava;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPingSender;
import org.eclipse.paho.client.mqttv3.internal.ClientComms;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
class SimpleCallback implements MqttCallback, MqttPingSender {
private String instanceData = "";
@Override
public void connectionLost(Throwable cause) { //Called when the client lost the connection to the broker
System.out.println("Connection lost on instance \"" + instanceData + "\" with cause \"" + cause.getMessage() + "\" Reason code " +
((MqttException)cause).getReasonCode() + "\" Cause \"" +
((MqttException)cause).getCause() + "\"");
cause.printStackTrace();
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("-------------------------------------------------");
System.out.println("| Topic:" + topic);
System.out.println("message arived "+new String(message.getPayload()));
System.out.println("-------------------------------------------------");
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {//Called when a outgoing publish is complete
}
//for ping request
@Override
public void init(ClientComms arg0) {
// TODO Auto-generated method stub
}
@Override
public void schedule(long arg0) {
// TODO Auto-generated method stub
}
@Override
public void start() {
// TODO Auto-generated method stub
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
}
public class mqttTest {
public static void main(String[] args){
String topic = "56";
String content = "Message form java";
int qos = 2;
String broker = "tcp://iot.eclipse.org:1883";
String clientId = "234";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId,persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(false);
// connOpts.setKeepAliveInterval(3000);
System.out.println("Connecting to broker: " + broker);
sampleClient.connect(connOpts);
sampleClient.subscribe(topic, 2);
System.out.println("Connected");
System.out.println("Publish message: " + content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
sampleClient.setCallback(new SimpleCallback());
sampleClient.publish(topic, message);
System.out.println("Message published");
try {
// Thread.sleep(5000);
// sampleClient.disconnect();
} catch(Exception e) {
e.printStackTrace();
}
// System.out.println("Disconnected");
// System.exit(0);
} 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("except " + me);
me.printStackTrace();
}
}
}
发生错误后几分钟:
Connection lost on instance "" with cause "Connection lost" Reason code 32109" Cause "java.net.SocketException: Connection reset"
Connection lost (32109) - java.net.SocketException: Connection reset
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.io.DataInputStream.readByte(DataInputStream.java:265)
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 more
答案 0 :(得分:3)
如果您使用标准&最新的Paho客户端,您不必实现ping请求模块。 Paho具有内置功能。您必须在建立连接时设置Keepalive间隔,并且Paho在后端处理PINGREQ和PINGRESP。
问题可能出在代理设置中,或尝试使用不同的keepalive值。