Created
November 25, 2015 07:31
-
-
Save randombenj/a6dfb1eca7337de9727f to your computer and use it in GitHub Desktop.
This websocket connection automaticaly reconnects when the closing event gets fired.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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