【问题标题】:Paho Rabitmqq connection getting failedPaho Rabitmqq 连接失败
【发布时间】:2016-02-09 11:11:56
【问题描述】:

这是我的paho 客户端代码

// 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

我错过了什么?

【问题讨论】:

    标签: rabbitmq mqtt paho


    【解决方案1】:

    根据我在 Windows 上使用 Paho MQTT JavaScript 库和 RabbitMQ 代理的个人经验,这里列出了您需要做的事情,才能在浏览器中使用 JS 的 MQTT:

    1. 安装rabbitmq_web_mqtt插件(你可能会找到最新的二进制文件here,将其复制到“c:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\plugins\”,并使用“rabbitmq-plugins enable”从命令行启用rabbitmq_web_mqtt”。
    2. 当然,MQTT插件也需要在broker上开启
    3. 对我来说,客户端无法使用 3.6.1 版的 RabbitMQ,但可以正常使用 3.6.2 版 (Windows)
    4. 用于连接的端口是 15675,而不是 1883!
    5. 确保在创建 Paho.MQTT.Client 实例时指定所有 4 个参数。如果你省略一个,你会得到 websocket 连接错误,这可能会产生误导。 最后,这是我测试过的代码 sn-p 并且运行良好(只是建立连接):

    	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】:

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

      这将通过 Websockets 建立 MQTT 连接(如错误所示)。这与基于 TCP 连接的本机 MQTT 不同。

      默认纯MQTT端口如果是1883,Websocket支持很可能在不同的端口上。

      您需要将 RabbitMQ 配置为接受 MQTT over Websockets 以及纯 MQTT,此 pull 请求 RabbitMQ 接缝讨论添加此功能。它提到此功能仅在 3.6.x 版本中添加,并且文档仍然未完成(截至 2016 年 2 月 9 日)

      【讨论】:

      • 您的意思是当前版本不支持它?你能告诉我如何在 rabitmq 中配置 MQTT
      • 拉取请求确实暗示此功能不在当前版本中。你必须使用 RabbitMQ 吗?您能否使用已经支持 Websockets 的其他代理,例如 mosquitto
      • 不,我还没有绑定任何经纪人。只是为移动聊天应用寻找一个不错的经纪人?那么另一个不错的选择是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      相关资源
      最近更新 更多