This is no exception either. Talk is cheap, I'll show you the code.
var newSocket = function() {
ws = new WebSocket(wsUri);
ws.onopen = function(evt) {
// send a request
if ($('#tag').val().length > 0) {
var req = {info: $('#tag').val()}
ws.send(JSON.stringify(req))
}
}
ws.onclose = function(evt) {
reconnectSocket()
}
ws.onmessage = function(evt) {
// do what you wanna do
}
ws.onerror = function(evt) {
}
};
var reconnectSocket = function() {
if (!ws || ws.readyState == WebSocket.CLOSED) {
newSocket()
}
};
$('#btn-go').click(function() {
reconnectSocket()
return false
})