我无法通过 Websocket 从 JavaScript 客户端连接到本地 Mosquitto 1.4.10 代理。
同一 JavaScript 客户端已通过 Websocket 在端口 8080 上成功连接到 test.mosquitto.org 上的公共(public)代理。
端口 1883 上的 MQTT 协议(protocol)连接工作正常,我使用 mosquitto_pub 和 mosquitto_sub 进行了测试。
我的代理是在运行 Ubuntu 14.04 的 VirtualBox 中设置的。
我在同一台虚拟机上安装了 libwebsockets。
我的本地代理是在 config.mk 文件中使用 WITH_WEBSOCKETS:=yes 进行编译的
我正在通过 Firefox 浏览器从同一虚拟机加载 JavaScript 客户端网页,并在浏览器控制台中看到以下错误消息:
Firefox can't establish a connection to the server at ws://localhost:8080/mqtt
我们将非常感谢您提出的解决此问题的建议。
谢谢。
这是我的 Mosquitto .conf 文件:
port 1883
listener 8080
protocol websockets
log_type all
websockets_log_level 1023
connection_messages true
这是 Mosquitto 服务器的日志(Websockets 日志记录级别设置为 1023,并且打开详细日志记录 - 当我加载 JavaScript 网页时不会出现任何消息):
1481381105: mosquitto version 1.4.10 (build date 2016-12-10 18:47:37+0530) starting
1481381105: Config loaded from /etc/mosquitto/mosquitto.conf.
1481381105: Opening websockets listen socket on port 8080.
1481381105: Initial logging level 10231481381105: Libwebsockets version: 2.1.0 manavkumarm@manav-alljoyn
1481381105: IPV6 not compiled in
1481381105: libev support not compiled in
1481381105: libuv support not compiled in
1481381105: Threads: 1 each 1024 fds
1481381105: mem: platform fd map: 4096 bytes
1481381105: Compiled with OpenSSL support
1481381105: Creating Vhost 'default' port 8080, 3 protocols, IPv6 off
1481381105: Using non-SSL mode
1481381105: Listening on port 8080
1481381105: mem: per-conn: 376 bytes + protocol rx buf
1481381105: canonical_hostname = mqtt
1481381105: Opening ipv4 listen socket on port 1883.
1481381105: Opening ipv6 listen socket on port 1883.
这是 JavaScript 源代码:
<html>
<body>
<script src="mqttws31.js"></script>
<script>
try
{
// Create a client instance
console.log("Creating client object...");
client = new Paho.MQTT.Client("localhost", Number(8080), "manav");
//client = new Paho.MQTT.Client("test.mosquitto.org", Number(8080), "manav");
// set callback handlers
console.log("Setting handlers...");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
console.log("Connecting...");
client.connect( {
onSuccess: onConnect,
mqttVersion: 4
});
}
catch (e)
{
console.log("Error: " + e.description);
}
// called when the client connects
function onConnect()
{
// Once a connection has been made, make a subscription and send a message.
console.log("Connected");
setTimeout( function() {
client.subscribe("world");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "world";
client.send(message);
//client.disconnect();
}, 5000);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("Connection lost: " + responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("Received Message: " + message.payloadString);
client.disconnect();
}
</script>
<h1>My MQTT Websockets Example</h1>
</body>
</html>
最佳答案
我发现我的回答有点晚了。
MQTT 的 websocket 端口是 1884 或其他端口,您有 8080。也许这就是问题所在。
8080不是保留的TCP端口吗?
另外,我知道你有 JavaScript 代码,但它是泛美卫生组织。我能够使发布者(它使用与订阅者相同的类,因此它也必须在订阅者端 - 这只是假设)与泛美卫生组织 python 客户端一起使用 websockets ,该客户端必须通过定义 进行初始化>传输参数。 -> 与浏览器通信(请参阅下面的 javascript)
mqtt.Client(transport='websockets')
保留该参数 MQTT 假定使用 TCP
mqtt.Client()
另外:
我的经纪人上的配置:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 1883
listener 1884
protocol websockets
我用 paho-mqtt 找到了我非常旧的 Javascript。它正在工作,所以我就把它放在这里。 它同时是订阅者和发布者。与配置一起,它的工作就像魅力一样。
class sub{
constructor(hostname,port,clientid,topic){
this.buffer = []
this.hostname=hostname;
this.port=port;
this.clientid = clientid;
this.topic = topic;
this.client = new Paho.MQTT.Client(hostname,port, clientid);
// set callback handlers
this.client.onConnectionLost = this.onConnectionLost;
this.client.onMessageArrived = this.onMessageArrived.bind(this);
// connect the client
this.client.connect({onSuccess:this.onConnect});
}
onConnect(){
console.log('OnConnect');
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
this.buffer.push(JSON.parse(message.payloadString));
}
subsribe(){
this.client.subscribe(this.topic)
}
publish(message){
console.log(message)
var mg = new Paho.MQTT.Message(JSON.stringify(message));
mg.destinationName = this.topic;
this.client.send(mg);
}
}
var x
x = new sub('xx.xx.xx.xx',1884,'clientID','LED');
function on(){
x.publish({'LED':'ON'});
}
function off(){
x.publish({'LED':'OFF'});
}
关于javascript - 无法通过 Websocket 连接到 Mosquitto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41076961/