Skip to content

Instantly share code, notes, and snippets.

@randombenj
Created November 25, 2015 07:31
Show Gist options
  • Save randombenj/a6dfb1eca7337de9727f to your computer and use it in GitHub Desktop.
Save randombenj/a6dfb1eca7337de9727f to your computer and use it in GitHub Desktop.
This websocket connection automaticaly reconnects when the closing event gets fired.
// gets called when a message is recieved
var onmessage = function (event) {
console.log(event);
};
// reconnecting websocket
var socket;
(function start(address){
// creating a WebSocket and set eventhandlers
socket = new WebSocket(address);
socket.onmessage = onmessage;
socket.onclose = function(){
// reconnect the websocket
setTimeout(function() {
start(address);
}, 100);
};
})('ws://url.to/web/socket');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment