One: The background of websokcet generation
Today, many Web sites are polled for the technology they use to implement push technology. Polling is at a specific time interval (such as every 1 seconds), the browser sends an HTTP request to the server, and then the server returns the latest data to the client's browser. This traditional pattern brings with it the obvious drawback that the browser needs to constantly make requests to the server, but the header of the HTTP request is very long, and the data contained in it may be a small value, which consumes a lot of bandwidth and server resources.
WebSocket is a protocol that HTML5 begins to provide full-duplex communication on a single TCP connection. The WebSocket communication protocol was established as the standard RFC 6455,websocketapi by the IETF in 2011. In the WebSocket API, the browser and server only need to do a handshake, and then a fast channel is formed between the browser and the server. The data can be transmitted to each other directly between the two.
Two: WebSocket Agreement and handshake
The format of the WebSocket protocol is ' ws://ip:port ' or ' wss://ip:port '. Where WSS represents the WebSocket protocol for encrypted transmissions.
The WebSocket protocol, like the traditional socket protocol, requires a "handshake". However, the "handshake" phase of websocket is done through the HTTP protocol, and the "handshake" behavior is accomplished through the Request/response header, which requires only a small exchange of data to create a duplex channel based on the TCP/IP protocol.
At the time of the handshake request, the client sends a GET request to the server and adds so many keys to the requested header
Origin:http://ip:port represents the address of the client
Connection:upgrade/upgrade:websocket says this request is for WebSocket handshake action.
Sec-websocket-version:13 represents the WebSocket version information supported by the browser
Sec-websocket-key: This is a randomly generated string by the client
In the handshake information for the server response sec-websocket-accept: The value of the server is computed and encrypted by the value of the Sec-websocket-key of the client header. And the server's response status of 101 indicates that the server side has understood the client's requirements, and the client needs to switch to the new protocol to complete subsequent communication based on the protocol type in upgrade. This is when our TCP/IP duplex channel is established.
Three: Websokcet Common methods and properties
Main methods:
1. Send () sends data to the remote server
2. Close () closes the websocket link
Listener functions:
OnOpen: After the handshake is complete and the TCP/IP channel is created
OnClose: after disconnecting
OnError: When an error occurs
OnMessage: When receiving a service-side message
ReadyState Properties Check Connection status
1, connecting (0) WebSocket is trying to establish a connection to the server
2, OPEN (1) WebSocket and the server has established a connection
3, CLOSING (2) WebSocket shutting down the connection to the server
4, CLOSED (3) WebSocket has closed the connection to the server
Websokcet principle and use