|
Project Information
Links
|
The Graceful WebSocketSo, you want to start building realtime event driven applications using the new HTML5 WebSocket API?
Introducing, the gracefulWebSocket jQuery plugin:
Contact: http://twitter.com/ffdead Basic UsageBasic usage is very straight forward. The whole purpose of this plugin is to enable developers to use the WebSocket API to write applications, even if the browser doesn't support it. Include plugin// include the plugin <script type="text/javascript" src="jquery.gracefulWebSocket.js"></script> Open WebSocket// open socket using standard syntax
var ws = $.gracefulWebSocket("ws://127.0.0.1:8080/");Sending// send message to server using standard syntax
ws.send("message to server");Receiving// listen for messages from server using standard syntax
ws.onmessage = function (event) {
var messageFromServer = event.data;
};Graceful degradation explainedThe gracefulWebSocket will automatically revert to fallback mode if no native WebSocket implementation can be detected in the browser. A fallback object implementing the same interface with be returned instead, which uses HTTP POST to send messages to the server, and polling HTTP GET requests to retrieve new messages from the server. // in fallback mode: connect returns a dummy object implementing the WebSocket interface
var ws = $.gracefulWebSocket("ws://127.0.0.1:8080/"); // the ws-protocol will automatically be changed to http// in fallback mode: sends message to server using HTTP POST
ws.send("same message to server, this time send using a POST request");// in fallback mode: listens for messages by polling the server using HTTP GET
ws.onmessage = function (event) {
var sameMessageFromServer = event.data;
};
|