My favorite web technology in the HTML5 specification is the WebSocket API, which is rapidly becoming popular. WebSocket provides a popular technology to replace the Ajax technology we have been using for the past few years. This new API provides a way to effectively push messages to the server using simple syntax from the client. Let's take a look at the HTML5 WebSocket API: it can be used on the client and server side. And there is an excellent third-party API called Socket.IO.
1. What is the WebSocket API in HTML5?The WebSocket API is the asynchronous communication method of the next generation client-server. This communication replaces a single TCP socket, using the ws or wss protocol, can be used for any client and server program. WebSocket is currently standardized by W3C. WebSocket is already supported by browsers such as Firefox 4, Chrome 4, Opera 10.70, and Safari 5.
The greatest thing about the WebSocket API is that the server and client can push information to each other at any time within a given time range. WebSocket is not limited to communication in Ajax (or XHR) mode, because Ajax technology requires clients to initiate requests, while WebSocket servers and clients can push information to each other; XHR is restricted by domains, while WebSocket allows cross-domain communication.
The smart thing about Ajax technology is that there is no way to design it to use. WebSocket is created for the specified target and is used to push messages in both directions.
2. Usage of WebSocket API in HTML5Focus only on client-side APIs, because each server-side language has its own API. The following code snippet is to open a connection, create an event listener for the connection, disconnect, message time, send a message back to the server, and close the connection.
Copy the code