Paho Rabitmqq connection getting failed

2019-09-10 00:26发布

Here is my paho client code

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

On Rabbitmq server everything is default seetings. When i run this code i get WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Connection closed before receiving a handshake response

What i am missing ?

2条回答
别忘想泡老子
2楼-- · 2019-09-10 01:04

It's not clear in the question but I assume you are running the code above in a web browser.

This will be making a MQTT connection over Websockets (as shown in the error). This is different from a native MQTT over TCP connection.

The default pure MQTT port if 1883, Websocket support is likely to be on a different port.

You will need to configure RabbitMQ to accept MQTT over Websockets as well as pure MQTT, this pull request for RabbitMQ seams to talk about adding this capability. It mentions that this capability was only added in version 3.6.x and that the documentaion is still outstanding (as of 9th Feb 2016)

查看更多
相关推荐>>
3楼-- · 2019-09-10 01:08

From my personal experience with Paho MQTT JavaScript library and RabbitMQ broker on windows, here is a list of things that you need to do to be able to use MQTT from JS from within a browser:

  1. Install rabbitmq_web_mqtt plugin (you may find latest binary here, copy it to "c:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\plugins\", and enable from command line using "rabbitmq-plugins enable rabbitmq_web_mqtt".
  2. Of course, MQTT plugin also needs to be enabled on broker
  3. For me, client was not working with version 3.6.1 of RabbitMQ, while it works fine with version 3.6.2 (Windows)
  4. Port to be used for connections is 15675, NOT 1883!
  5. Make sure to specify all 4 parameters when making instance of Paho.MQTT.Client. In case when you omit one, you get websocket connection error which may be quite misleading. Finally, here is a code snippet which I tested and works perfectly (just makes connection):

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

查看更多
登录 后发表回答