0

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>
6
  • What broker are you using? Is it one that supports websockets? Commented Jul 2, 2015 at 5:47
  • I tried using Mosquitto broker then i ran behind issues, then i replaced with HiveMQ broker, with that I simply enabled websocket and got it fixed But the thing is i read mosquitto 1.4 above supports websocket the i made the same changes in mosquitto broker as well, but still it is down Commented Jul 2, 2015 at 6:48
  • Your still connecting to the wrong port. In the other question the websocket port we set up for mosquitto was 1884 not 1883 Commented Jul 2, 2015 at 8:16
  • with HiveMQ broker i did set listener port as 8000 for websocket and it is working fine now.. Sorry for not adding this info and also i removed reconnecting code Commented Jul 2, 2015 at 9:18
  • Plz help me with Mosquitto broker Commented Jul 2, 2015 at 9:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any other alternative for me to use mosquitto broker for javascript client,?
I'm not sure what you're asking. The mosquitto broker doesn't act as a javascript client. You can use any javascript MQTT client with mosquitto, but you'll have to compile it yourself on Windows to get websockets support.
In java i was using tcp for mqtt .. here with javascript should i depend on websocket only or is any way to connect using tcp? Sorry Iam new to networking stuffs like tcs/websocket and so on :(
Websockets works on top of tcp. You could implement a javascript client that used straight sockets as well of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.