Error Encountered: WebSocket Error 12031 - The Server Connection Was Reset
In order to subscribe to MQTT messages from the user interface, the code below is being utilized. A Mosquitto broker is currently running on my local machine, with the IP address set as the URL and listening on port 1883. A random Client ID has also been provided.
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../paho.javascript-1.0.1/mqttws31-min.js"></script>
<script src="../paho.javascript-1.0.1/mqttws31.js"></script>
<script src="../js/browserMqtt.js"></script>
<script>
// Create a client instance
client = new Paho.MQTT.Client("10.9.177.110", 1883, "100");
// 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("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>