如何使用Websockets连接到MQTT Broker?

时间:2020-01-09 15:29:03

标签: javascript websocket mqtt

我有一个用Chrome打开的test.html文件:

<html>   
<head>
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>

<body>
    <script>
        var client = mqtt.connect('ws://broker.hivemq.com:8000')
        client.subscribe("mqtt/demo")

        client.on("message", function (topic, payload) {
            alert([topic, payload].join(": "))
            client.end()
        })

        client.publish("mqtt/demo", "hello world!")
    </script>
</body>
</html>

不幸的是我遇到这个错误:

mqtt.min.js:1 WebSocket connection to 'ws://broker.hivemq.com:8000/' failed: Connection closed before receiving a handshake response

我也尝试使用mqtt://,但它不起作用。任何想法?

1 个答案:

答案 0 :(得分:1)

此服务器已停止或关闭,无法访问。请使用

ws://broker.emqx.io:8083/mqtt

ws://broker.mqttdashboard.com:8000/mqtt

尝试