WebSocket Communication Protocol (10 version) Introduction

Source: Internet
Author: User
Tags emit

Objective:

Work in the WebSocket Protocol 10 version of the agreement in English, see here:

Http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10

This article is equivalent to a summary of the work.

First of all, you need to briefly understand why the birth of WebSocket communication protocol, the Web communication is generally based on HTTP (Hypertext Transfer Protocol) communication, so there is no way to establish a long network connection, the general communication is this:

Request

Browser---------------------> Server

<-----------------------

Response

This connection is initiated by the client, and the connection is closed after the server replies to the data.

As if you use a browser to access Baidu input www.baidu.com, the browser initiated the request, Baidu's server will be the HTML hypertext of the page to your browser and close the connection.

This connection time is very short, and the server is unable to actively transfer data.

For a few examples:

Youku, potatoes These websites can play movies on the internet, the movie needs to continue to transmit data, it can only embed Flash player, flash socket in flash to continue to transmit data.

Using HTML, JS, etc. to write a timely chat software is very difficult, the key lies in the inability to establish a continuous connection, the server can not actively transmit data to the client. Only for a period of time the client initiates a request to actively ask the server if there is any data?

WebSocket can establish a stable connection, can solve the above problems.

First of all, the principle, will later put the code files to download for everyone.

WebSocket Communication Process:

1. Client initiates connection request

The WebSocket client first initiates a connection request and sends the following data format:

get/10.15.1.218:12345/chat?key=value\r\n

http/1.1\r\n upgrade:websocket\r\n connection:upgrade\r\n host:10.15.1.218:12345\r\n sec-websocket-origin:null\r\n sec-websocket-key:4tajitqo9so2wu8lkrsq3w==\r\n sec-websocket-version:8\r\n\r\n

This is similar to the HTTP header, note that each line of data end Terminator is "\ r \ n", the last Terminator is "\r\n\r\n".

The 1th line of the request header is detailed:

"GET/" is followed by the server's IP and port (10.15.1.218:12345) must have. '/' is followed by your own string, (chat), whatever you preach, this part is optional. String '? ' The following are some parameters (Key=value), what you define yourself, and this part is also optional. All three of these are legal as follows:

get/10.15.1.218:12345\r\n

Or

get/10.15.1.218:12345/chat\r\n

Or

get/10.15.1.218:12345/chat?key=value\r\n

2nd, 3, 4, 5, 6 lines:

http/1.1\r\n upgrade:websocket\r\n connection:upgrade\r\n host:10.15.1.218:12345\r\n sec-websocket-origin:null\r\n

These are basically fixed formats and content, Host: The IP and port behind the server (the connected person).

Line 7th:

sec-websocket-key:4tajitqo9so2wu8lkrsq3w==\r\n
Sec-websocket-key back of the string of things, the strings of length 24 is randomly generated by the client, we call him Cli_key, the server must use it through a certain operational rules to generate server-side key, temporarily called Ser_key, and then Ser_ Key sends back, the client verifies correctly after the handshake succeeds!

Line 8th:

sec-websocket-version:8\r\n\r\n

I'm not quite sure why the version is 8. In the 10 version of the communication protocol, the client sends 8.

The WebSocket client is implemented in the Chrome 14 browser without its own implementation. can go to download one when WebSocket client is used.

2. Create a service-side key

Our server intercepts the Key1 (length 24)

4tajitqo9so2wu8lkrsq3w==

With it and a custom string (length 36):

258eafa5-e914-47da-95ca-c5ab0dc85b11

Connect them like this:

4tajitqo9so2wu8lkrsq3w==258eafa5-e914-47da-95ca-c5ab0dc85b11

The long string is then encrypted by the SHA-1 algorithm, and the binary data with a length of 20 bytes is obtained.

The data is then BASE64 encoded, and finally the server-side key, which is Ser_key:

beveglzrb9fs3rj8wzexjdcsedg=

3. Server Return key

Then you need to return the key to the client, complete the handshake, and send the following data format:

http/1.1 101 Switching protocols\r\n

upgrade:websocket\r\n

connection:upgrade\r\n

sec-websocket-accept:beveglzrb9fs3rj8wzexjdcsedg=\r\n\r\n

At this point, the handshake was successful!

4. Transfer data (simple introduction data length less than 126 of the data transmission, transmission is greater than or equal to 126 bytes of the head (head) can be more than 2 bytes, to see the English document in the section on the head)

Must have a mask

Client-----------------------> Server

<-----------------------

Mask (optional)

The protocol specifies that the data that the client sends to the server must have a mask, such as the need to send a string "Hello",

ASCII code for "Hello":

H e l l o

Hex 0x48 0x65 0x6c 0x6c 0x6f

Decimal 72 101 108 108 111

But the actual data sent is this:

------------------Head-----Mask 0----1-------2-------3-----H----e-----l------l-------o--------------

0x81 0x85 0x37 0xfa 0x21 0x3d 0x7f 0x9f 0x4d 0x51 0x58

Head heads I'm not here to say, need a lot of text to explain, go to the English agreement in the relevant places to see it, the head is a 4-byte mask, randomly generated, and then the data, you may find that the data has changed, the data is the ASCII code of Hello and the mask to do the XOR or calculation.

The arithmetic rule is this, the 0th character ' H ' and the 0th mask xor, the first character ' E ' and the first mask xor ...

In fact, the C language means res = str[n]^mask[n%4]

The server emits data that can have a mask or no mask

Issuing a "Hello" string can emit the same data as the client, or it can emit a mask like the following:

0x81 0x05 0x48 0x65 0x6c 0x6c 0x6f H e l l o

This is the header information plus the original data.

5. Close the connection

This part is very simple, perhaps you can go to the English agreement to find it to take a look.

Related Article

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.