Extended WebSocket library for Javascript, which allows subscribing to events along with handling various events with callbacks.
WebSocketEx.prototype.onopen
WebSocketEx.prototype.onclose
WebSocketEx.prototype.onretry
WebSocketEx.prototype.onmessage
WebSocketEx.prototype.subscribe
WebSocketEx.prototype.send
var socket = new WebSocketEx();
var socket = new WebSocketEx('localhost'); // default
var socket = new WebSocketEx('localhost', 8080); // default
WebSocketEx.prototype.subscribe
is fired whenever a JSON-encoded string is sent to the client, in the following format:
{
'type': 'event name',
'data': your_object_here
}
window.socket = new WebSocketEx();
window.socket.onopen(function() {
console.log('Connected to server!');
window.socket.send('Sending some data!');
});
window.socket.onretry(function() {
console.log('Lost connection to websocket server, attempting to reconnect...');
});
window.socket.subscribe('alert', function(data) {
console.log('Do something with your ' + data.customProperty + ', or it could be just a simple string that says: ' + data);
});
window.socket.onmessage(function(e) {
console.log('Here's a socket message! Published events will never reach this. They are handled by WebSocketEx.prototype.subscribe.');
});