javascript - 无法通过 Websocket 连接到 Mosquitto

标签 javascript virtualbox mqtt mosquitto libwebsockets

我无法通过 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。

我的本地经纪人是用 编译的WITH_WEBSOCKETS:=是在 config.mk 文件中

我正在从 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 1023

1481381105: 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 代码,但它的 paho。我能够让发布者(它使用与订阅者相同的类,因此它也必须在订阅者端 - 虽然这只是假设)在 上工作网络套接字 使用必须通过定义 初始化的 paho python 客户端运输 范围。 -> 与浏览器通信(请参阅下面的 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/

相关文章:

android - CNNAN0020W 套接字 4 上的套接字错误,对等地址 <IP 地址> :57710; ending connection

javascript - NodeJS使用Puppeteer处理弹出窗口

linux - 自动启动 Vagrant Jekyll

laravel - Vagrant Up VT-X 不可用

windows - 从 vbox 复制/粘贴到 windows

c++ - MQTT C++客户端

ssl - 为什么使用SSL的Websocket上的MQTT无法正常工作

javascript - 如何选择一个类而不选择其内部元素?

javascript - getElementsByClassName Iexplorer 兼容性

javascript - 网页如何防止粘贴事件?