suma shetty
suma shetty

Reputation: 49

Unable to connect to mqtt broker using javascript client

I am getting the error

SCRIPT12152: WebSocket Error: Network Error 12152, The server returned an invalid or unrecognized response

in IE , and

WebSocket connection to 'ws://192.168.1.100:1883/' failed: Connection closed before receiving a handshake response

in chrome.. below is the piece of code I have used

<!DOCTYPE html>
<html lang="en">

<head></head>

<body>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../js/mqttws31.js"></script>
<script src="../js/mqttws31-min.js"></script>
<script src="../js/reconnecting-websocket.js"></script>
<script src="../js/reconnecting-websocket.min.js"></script>
<script>

// Create a client instance
client = new Paho.MQTT.Client("192.168.1.100", 1883, "100");
var s = new ReconnectingWebSocket("ws://192.168.1.100:1883");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

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

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

</body>

</html>

Upvotes: 0

Views: 1620

Answers (1)

ralight
ralight

Reputation: 11608

If you're using the binaries for Windows provided by mosquitto, you should be aware that they do not come with libwebsockets support enabled. If you want websockets support with mosquitto on Windows, you will need to compile libwebsockets yourself, then compile mosquitto after enabling websockets support.

It's also worth noting that currently libwebsockets support on Windows isn't that great, in particular the number of connected clients is limited to 64.

Upvotes: 2

Related Questions