首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Paho Rabitmqq连接失败

Paho Rabitmqq连接失败
EN

Stack Overflow用户
提问于 2016-02-09 11:11:56
回答 2查看 1.5K关注 0票数 0

这是我的paho客户端代码

代码语言:javascript
复制
// Create a client instance
client = new Paho.MQTT.Client('127.0.0.1', 1883, "clientId");

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});


// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  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);
}  

Rabbitmq服务器上,一切都是默认的。当我运行这段代码时,我得到了WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Connection closed before receiving a handshake response

我错过了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-09 11:28:04

问题中还不清楚,但我假设您是在web浏览器中运行上面的代码。

这将在Websockets上建立MQTT连接(如错误中所示)。这与TCP连接上的本机MQTT不同。

默认的纯MQTT端口(如果1883年,Websocket支持)可能位于不同的端口上。

您需要配置RabbitMQ以通过Websockets接受MQTT,以及纯MQTT,这是对RabbitMQ接缝的请求,以讨论如何添加此功能。它提到,此功能仅在3.6.x版本中添加,文档仍未完成(截至2016年2月9日)

票数 1
EN

Stack Overflow用户

发布于 2016-07-19 07:27:56

根据我在windows上使用Paho MQTT JavaScript库和RabbitMQ broker的个人经验,下面列出了您需要做的事情,以便能够在浏览器中使用JS中的MQTT:

  1. 安装rabbitmq_web_mqtt插件(您可以找到最新的二进制这里,将其复制到"c:\Program \rabbitmq\rabbitmq_server-3.6.2\plugins\“,并使用"rabbitmq-plugins启用rabbitmq_web_mqtt”从命令行启用。
  2. 当然,MQTT插件也需要在代理上启用。
  3. 对我来说,客户端没有使用RabbitMQ的3.6.1版本,而在3.6.2版(Windows)中运行得很好。
  4. 用于连接的端口是15675,而不是1883年!
  5. 在创建Paho.MQTT.Client实例时,请确保指定所有4个参数。如果您省略了一个,您会得到websocket连接错误,这可能会非常误导。最后,这里是我测试并完美工作的代码片段(只需要连接):

代码语言:javascript
复制
	client = new Paho.MQTT.Client("localhost", 15675, "/ws", "client-1");

	//set callback handlers
	client.onConnectionLost = onConnectionLost;
	client.onMessageArrived = onMessageArrived;

	//connect the client
	client.connect({
		onSuccess : onConnect
	});

	//called when the client connects
	function onConnect() {
		console.log("Connected");
	}

	//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);
	}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35290526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档