0

我收到错误

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

在 IE 中,和

WebSocket 连接到“ws://192.168.1.100:1883/”失败:连接在收到握手响应之前关闭

在 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>
4

1 回答 1

2

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

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

于 2015-07-02T15:30:28.997 回答