One simple interface $.WebSocket(url, protocol, options); thats it. The same interface as current native WebSocket implementation. The same native events (onopen, onmessage, onerror, onclose) + custom event onsend.
But jquery.WebSockets adds some nice features:
[x] Multiplexing - Use a single socket connection and as many logical pipes within as your browser supports. All these pipes are emulated WebSockets also with the same API + same events! Use each pipe as WebSocket! But this requires you to implement the protocol on this level of communication The data is en- + decoded in a special way to make multiplexing possible
[x] Interface for adding protocol to manipulate data before they are send and right after they arrive before event onmessage is fired!
example:
// The WebSocket-Object (with resource + fallback)
var ws = $.WebSocket ('ws://127.0.0.1:9300', null, {http: 'http://127.0.0.1:81/Lab/Websocket/Data/poll.php'});
// WebSocket onerror event triggered also in fallback
ws.onerror = function (e) {
console.log ('Error with WebSocket uid: ' + e.target.uid);
};
/**
* demonstrate multiplexing
*/
var pipe1;
// if connection is opened => start opening a pipe (multiplexing)
ws.onopen = function () {
//
pipe1 = ws.registerPipe ('user/all', null, {
onopen: function () {
console.log ('pipe1 (' + this.uid + ') connected!');
},
onmessage: function (e) {
console.log ('< pipe1 : ' + e.data);
},
onerror: function (e) {
console.log ('< pipe1 error : ' + e.data);
},
onclose: function () {
console.log ('pipe1 (' + pipe.uid + ') connection closed!');
}
});
};
Thanks to our sponsors and supporters:
JetBrains | Navicat |
---|---|