当我尝试通过这个 JS 连接到 Mosquitto 服务器时,我收到这个错误:
WebSocket connection to 'ws://xx.xxx.xxx.xxx:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
我从控制台尝试过,它工作正常。我尝试使用 Java 客户端作为提供者和消费者,它工作正常。所以我不明白我应该怎么做才能让它工作。
这是 JS:
client = new Paho.MQTT.Client("xx.xxx.xxx.xxx", 1883, "clientId");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
这是Java(工作):
@Stateless
@LocalBean
public class PahoManager implements MqttCallback {
MqttClient client;
public void init() {
try {
client = new MqttClient("tcp://192.168.50.10:1883", "pahomqttpublish1");
client.connect();
client.subscribe("pahodemo/test");
} catch (MqttException e) {
e.printStackTrace();
}
}
public void doDemo() {
try {
MqttMessage message = new MqttMessage();
message.setPayload("Ciao Pluto".getBytes());
client.publish("pahodemo/test", message);
//client.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void connectionLost(Throwable thrwbl) {
System.out.println("Connection Lost");
}
@Override
public void messageArrived(String string, MqttMessage mm) throws Exception {
System.out.println("Message: " + string + mm);
}
@Override
public void deliveryComplete(IMqttDeliveryToken imdt) {
System.out.println("Message delivered");
}
}