Julian
I do something like this
var wsUri = "your websocket url";
function start(wsUri) { // Create the websocket
console.log("CONNECT TO",wsUri);
ws = new WebSocket(wsUri);
ws.onopen = function(evt) {
console.log("CONNECTED");
};
ws.onclose = function(evt) {
console.log("DISCONNECTED");
setTimeout(function(){ start(wsUri) }, 3000); // try to reconnect every 3 secs... bit fast ?
}
ws.onmessage = function (evt) {
console.log("MESSAGE",evt);
// do your stuff here
}
ws.onerror = function(evt) {
console.log("ERROR",evt);
}
}
start(wsUri); // Connect the Websocket - to the URL