HTML5 Web Sockets is only one hand to revolutionize the web technologies as we know it. The purpose of this post is to explain what techniques have been used to simulate push far servers, define HTML5 web sockets, and then give an example of how to use it in your HTML5 application.
What is polling?
Until now, the web was an executive. In other words, web pages can only send a request to a web server, and not vice versa. Once AJAX came in 05, web developers have quickly adopted the techniques to simulate a request from the client server known as polling. There are two types of polls, in short polling and long polling.
short polling is implemented by making a request to the web server every few seconds or so to see if the data has changed. If so, the Web server will respond with the new data. Otherwise, it will respond with a blank message. The disadvantage of this technique, however, is both a surplus requests to the server and use a CPU overhead of the Web server to constantly check whether you have made a data update.
long polling is implemented by making a single request to the web server and keep the connection open until the data is changed, the web server will then send back a response. The disadvantage of this technique, such as short surveys, is that the Web server must also be ascertained whether the data is changed every few seconds or so, the creation of an overhead of CPU usage.
What is a HTML5 web outlet?
This is where HTML5 web sockets are available. HTML5 will be the first HTML specification to support client-side web outlets. In other words, when the data changes on the web server, the web server may send a request to the customer, eliminating the need for polling.
as HTML5 web socket
Step 1: Create a WebSocket with a valid URL
Create a new connection to the WebSocket WebSocket server finance.example.com.
stockTickerWebSocket var = new WebSocket ( "WS: //finance.example.com");
Note that a ws: // and WSS: // prefix indicate a WebSocket and a secure WebSocket connection respectively. The default port for WebSockets is 81 and the default port for secure WebSocket is 815.
Step 2: Connect JavaScript callback functions
event listener associated to manage every phase of the life cycle of connection.
stockTickerWebSocket.onopen = function (evt) {
alert ( "Stock Ticker Open Connection ...");
};
stockTickerWebSocket.onmessage = function (evt) {
alert ( "Received Update Ticker:" + evt.data);
};
stockTickerWebSocket.onclose = function (evt) {
alert ( "Connection closed.");
};
Step 3: send and receive data
To send a message to the server, you simply call the method on postMessage webocket with the content you want to send to the server.
stockTickerWebSocket.postMessage ( "BUY: GOOG, 100 @ 0.25");
This will send the message to the server BUY. Any message from the server will be delivered to the onMessage callback recorded in step # 2.
Step 4: Disconnect When Done
When completed, calling the disconnect () method to close the WebSocket connection .
stockTickerWebSocket.disconnect ();
As shown in the example above, there are no HTTP requests to the server from the client side to retrieve the data, instead of the data has been pushed to the client from the server - when it becomes available.
When a new WebSocket connection is established the browser opens an HTTP connection to the server and then negotiates with the server to update the link to a dedicated and persistent WebSocket connection. This process automatically sets a tunnel through the server - through all the network agents (proxies, routers and firewalls) in the center (much like HTTPS to establish a secure, endtoend connection), solving many problems that the various programming techniques encountered Comet . Once the WebSocket is a full-duplex channel between the client and the server.
provided by vBulletin Example Http://www.indicthreads.com
0 Komentar