无法使用javascript客户端连接到mqtt代理

时间:2015-07-01 17:31:04

标签: windows websocket mqtt mosquitto hivemq

我收到错误

  

SCRIPT12152:WebSocket错误:网络错误12152,服务器返回无效或无法识别的响应

IE中的

  

与'ws://192.168.1.100:1883 /'的WebSocket连接失败:在收到握手响应之前连接已关闭

在chrome ..下面是我使用过的代码

<!DOCTYPE html>
<html lang="en">

<head></head>

<body>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../js/mqttws31.js"></script>
<script src="../js/mqttws31-min.js"></script>
<script src="../js/reconnecting-websocket.js"></script>
<script src="../js/reconnecting-websocket.min.js"></script>
<script>

// Create a client instance
client = new Paho.MQTT.Client("192.168.1.100", 1883, "100");
var s = new ReconnectingWebSocket("ws://192.168.1.100:1883");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});

// called when the client connects
function onConnect() {
    alert("connected");
    // Once a connection has been made, make a subscription and send       
    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);
} 
</script>

</body>

</html>

1 个答案:

答案 0 :(得分:2)

如果你正在使用mosquitto提供的Windows二进制文件,你应该知道它们没有启用libwebsockets支持。如果你想在Windows上使用mosquitto支持websockets,你需要自己编译libwebsockets,然后在启用websockets支持后编译mosquitto。

值得注意的是,目前Windows上的libwebsockets支持并不是很好,特别是连接客户端的数量限制为64个。