这是我的paho客户端代码
// Create a client instance
client = new Paho.MQTT.Client('127.0.0.1', 1883, "clientId");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("/World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
} 在Rabbitmq服务器上,一切都是默认的。当我运行这段代码时,我得到了WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Connection closed before receiving a handshake response
我错过了什么?
发布于 2016-02-09 11:28:04
问题中还不清楚,但我假设您是在web浏览器中运行上面的代码。
这将在Websockets上建立MQTT连接(如错误中所示)。这与TCP连接上的本机MQTT不同。
默认的纯MQTT端口(如果1883年,Websocket支持)可能位于不同的端口上。
您需要配置RabbitMQ以通过Websockets接受MQTT,以及纯MQTT,这是拉对RabbitMQ接缝的请求,以讨论如何添加此功能。它提到,此功能仅在3.6.x版本中添加,文档仍未完成(截至2016年2月9日)
发布于 2016-07-19 07:27:56
根据我在windows上使用Paho MQTT JavaScript库和RabbitMQ broker的个人经验,下面列出了您需要做的事情,以便能够在浏览器中使用JS中的MQTT:
client = new Paho.MQTT.Client("localhost", 15675, "/ws", "client-1");
//set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
//connect the client
client.connect({
onSuccess : onConnect
});
//called when the client connects
function onConnect() {
console.log("Connected");
}
//called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
//called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
}
https://stackoverflow.com/questions/35290526
复制相似问题