Share one's WebSocket weekly

Source: Internet
Author: User

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 :

    1. Get/chat http/1.1
    2. Host:server.example.com
    3. Upgrade:websocket
    4. Connection:upgrade
    5. sec-websocket-key:x3jjhmbdl1ezlkh9gbhxdw==
    6. Sec-websocket-protocol:chat,superchat
    7. Sec-websocket-version:13
    8. 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后的响应
    1. http/1.1 101 Switching protocols
    2. Upgrade:websocket
    3. Connection:upgrade
    4. sec-websocket-accept:hsmrc0smlyukagmm5oppg2hagwk=
    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.