无法使用Mosquitto Broker javascript客户端建立websocket连接

0 websocket mqtt

我正在我的Windows机器上运行MQTT服务器(mosquitto).该服务在端口号1883上运行.

从mosquitto.org下载mosquitto.js文件并进行如下调用当我调试时,我看到结果"connection.readyState == 0".如果我错过了什么,请帮助我.我正在使用chrome和safari最新版本来测试它.提前致谢.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/JavaScript" src="./js/mosquitto-1.1.js"></script> 

<title>publisher</title>

</head>

<body>
    <table align="center">
        <tr>
            <td>
                <h1>Publisher</h1>
                <table>
                    <tr>
                        <td><textarea rows="5" cols="25" id="txtMsg"></textarea></td>
                    </tr>
                    <tr>
                        <td align="center"><input type="button" value="post"
                            onclick="javaScript:postMessage();" /></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>
<script type="text/javascript">
function postMessage()
{

    var postVal = document.getElementById('txtMsg').value;
    var t = new Mosquitto();
    t.connect('ws://localhost',100000);
    t.publish('inbox/msgrec',postVal,0,0);

}
Run Code Online (Sandbox Code Playgroud)

我是否有必要安装jetty服务器或使用node.js使用mosquitto javascript客户端进行连接,或者是否有必要在mosquitto安装中对配置文件进行任何更改.

ral*_*ght 7

Mosquitto不直接支持WebSockets.您需要其他功能,并且会将携带MQTT数据包的WebSocket连接(例如由mosquitto.js生成)转换为原始MQTT.

test.mosquitto.org上的服务器将lighttpd作为web服务器运行,mod_websockets提供WebSockets支持.可以使用此方法连接到ws://test.mosquitto.org/mqtt.您需要自己创建一个类似的解决方案.Apache也有websocket模块可以执行此操作,或者您可以使用libwebsockets创建自己的websocket服务器.

  • 这已经过时了 - test.mosquitto.org正在侦听端口8080上的websockets. (2认同)