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>
ralight
  • 11,033
  • 3
  • 49
  • 59
  • What broker are you using? Is it one that supports websockets? – knolleary Jul 02 '15 at 05: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 – suma shetty Jul 02 '15 at 06:48
  • Your still connecting to the wrong port. In the other question the websocket port we set up for mosquitto was 1884 not 1883 – hardillb Jul 02 '15 at 08: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 – suma shetty Jul 02 '15 at 09:18
  • Plz help me with Mosquitto broker – suma shetty Jul 02 '15 at 09:19
  • Update the question with your mosquitto config file – hardillb Jul 02 '15 at 12:15

1 Answers1

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.

ralight
  • 11,033
  • 3
  • 49
  • 59
  • Is there any other alternative for me to use mosquitto broker for javascript client,? – suma shetty Jul 02 '15 at 19:17
  • 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. – ralight Jul 02 '15 at 19:46
  • 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 :( – suma shetty Jul 03 '15 at 07:18
  • Websockets works on top of tcp. You could implement a javascript client that used straight sockets as well of course. – ralight Jul 03 '15 at 07:55