A: What is WebSocket?
WebSocket is the new agreement HTML5 out, The WebSocket protocol supports two-way communication between a client running untrusted code in a controlled environment and a remote host that has selected the code to communicate.
Simply translate this passage: The browser makes a websocket request, the server responds, the browser and the server only need to do a handshake, and then the browser and the server form a fast channel. The data can be transmitted to each other directly between the two.
Two: Advantages over traditional HTTP requests
familiar with the HTTP protocol know that the server is passive, if the client wants to obtain data multiple times need to continue to request requests to the server, the server returned the response response, and then closed the connection, but in the http1.1 has been improved, so that there is a keep-alive, that is, in an HTTP connection, you can send multiple request, receive multiple response. But remember that request = Response, which is always the case in HTTP, which means that a request can have only one Response. And this response is also passive, can not initiate.
The WebSocket protocol solves this problem,when the server completes the Protocol upgrade (Http->websocket), the service side can actively push the message to the client. It also solves the problem of very resource depletion. Why do traditional HTTP requests go back to consuming resources? In fact, we use the program is to go through two layers of proxy, that is, HTTP protocol in Nginx and other servers, and then sent to the corresponding handler (PHP, etc.) to deal with. In the Nginx and other server parser is still very fast, but in the handler part of the processing is often very slow speed. Three: Parse a complete websocket handshake
WebSocket requests sent by the browser :
- Get/chat http/1.1
- Host:server.example.com
- Upgrade:websocket
- Connection:upgrade
- sec-websocket-key:x3jjhmbdl1ezlkh9gbhxdw==
- Sec-websocket-protocol:chat,superchat
- Sec-websocket-version:13
- Origin:http://example.com
which
Upgrade:websocket
Connection:upgrade
These two statements declare that this is a websocket request, telling Nginx,apache and other servers to use the WebSocket protocol.
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13
Sec-WebSocket-Key 是一个Base64 encode的值,这个是浏览器随机生成的,用来发送给服务器,用来验证是否是真的webSocket请求。
Sec_WebSocket-Protocol 是一个用户定义的字符串,用来区分同URL下,不同的服务所需要的协议。
Sec-WebSocket-Version 是告诉服务器所使用的Websocket Draft(协议版本)。
服务器收的webSocket后的响应:
- http/1.1 101 Switching protocols
- Upgrade:websocket
- Connection:upgrade
- sec-websocket-accept:hsmrc0smlyukagmm5oppg2hagwk=
- Sec-websocket-protocol:chat
The server still returns these two words after receiving the request
Upgrade: websocket
Connection: Upgrade
Tell the client that the WebSocket protocol is about to be upgraded.
sec-websocket-accept This is confirmed by the server, And the Sec-websocket-key returned by the server after encryption.
sec-websocket-protocol Is the protocol that represents the end use.
IV: an implementation of WebSocket Instance
Syntax:
var ws=newWebSocket(url,[protocol]) ;
Ws.onopen = function () {
"WebSocket connected, sending data using the Send () method"
Ws.send (' send data ');
}
Ws.onmessage = function (evt) {
"Receive Data"
var reseved_mes = Evt.data;
}
Ws.onclose = function () {
"Close WebSocket"
}
as for how to use WebSocket on clients that do not support websocket. The answer is: no
Share one's WebSocket weekly