Reputation: 101
I'm trying to connect Paho JS lib to Snap! to allow communication with server through MQTT protocol, but somehow WebSockets keep crashing on me.
Here's the code i use:
var wsbroker = "127.0.0.1";
var wsport = 9001;
console.log("Connecting to: ", wsbroker);
console.log("Connecting to port: ", Number(wsport));
client = new Paho.MQTT.Client(wsbroker, Number(wsport),"Snap");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
var client_options = {
onSuccess:onConnect,
onFailure:doFail
}
client.connect(client_options);
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("Client connected...");
client.subscribe("/Navicula/test");
message = new Paho.MQTT.Message("CONNECTED");
message.destinationName = "/Navicula/test";
client.send(message);
}
function doFail(e){
console.log("I haz failed");
console.log(e);
}
// 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);
}
mqttws31.min.js is implemented before (used mqttws31.js but the same results).
I have mosquitto 1.4.8 installed with following config:
# 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
listener 1883 127.0.0.1 protocol mqtt
listener 9001 127.0.0.1 protocol websockets
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
The JS code runs from html file on pythons SimpleHTTPServer and after running always prints this in console:
WebSocket connection to 'ws://127.0.0.1:9001/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
with this as error code on onFail function:
Object {invocationContext: undefined, errorCode: 7, errorMessage: "AMQJS0007E Socket error:undefined."}
On the server side (mosquitto) i get following log:
1496269205: mosquitto version 1.4.8 (build date Fri, 19 Feb 2016 12:03:16 +0100) starting
1496269205: Config loaded from myconf.conf.
1496269205: Opening ipv4 listen socket on port 1883.
1496269205: Opening ipv4 listen socket on port 9001.
1496269215: New connection from 127.0.0.1 on port 9001.
1496269215: Socket error on client <unknown>, disconnecting.
1496269215: New connection from 127.0.0.1 on port 9001.
1496269215: Socket error on client <unknown>, disconnecting.
1496269224: New connection from 127.0.0.1 on port 9001.
1496269224: Socket error on client <unknown>, disconnecting.
1496269224: New connection from 127.0.0.1 on port 9001.
1496269224: Socket error on client <unknown>, disconnecting.
So i guess it must be fault somewhere in the code part, but i'm totally lost here.
Upvotes: 0
Views: 3386
Reputation: 59658
The protocol
definition should be on a separate line to the listener
in the configuration file. At the moment they are being ignored so both listeners are being interpreted as native MQTT.
pid_file /var/run/mosquitto.pid
listener 1883 127.0.0.1
protocol mqtt
listener 9001 127.0.0.1
protocol websockets
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
Upvotes: 0