Websocket packet Protocol

Source: Internet
Author: User
Tags sha1

In fact, I have been wondering why HTML5 wraps an application layer discussion as the basic purpose of socket adoption. In fact, directly supporting socket TCP is comparatively simpler and more flexible. since the standards have been developed and the browser also supports them, we can only use points for our developers. the latest version of websocket Protocol onIts standard specification has been clearly defined, So now can be developed according to this standard. For details, refer to the http://datatracker.ietf.org/doc/rfc6455? Include_text = 1

The websocket protocol is divided into two parts: the first part is the connection license verification and the verified data interaction. the connection license verification is relatively simple. The client sends a request similar to HTTP. After the server obtains the request, it generates the corresponding value based on the request key and returns it.

Connection Request content:

 
GET/HTTP/1.1 connection: upgradehost: 127.0.0.1: 8088 origin: nullsec-websocket-extensions: X-WebKit-Deflate-framesec-websocket-key: puvouwb7rel6z2avzbknfw = sec-websocket-Version: 13 upgrade: websocket

After receiving the request, the server mainly targetsIf the sec-websocket-key is generatedThe key of sec-websocket-accept, which is generatedSec-websocket-acceptSha1 (sec-websocket-Key +258eafa5-e914-4710995ca-c5ab0dc85b11), C #CodeAs follows:

 
Sha1 sha1 = new sha1cryptoserviceprovider (); byte [] bytes_sha1_in = encoding. utf8.getbytes (request. secwebsocketkey + "258eafa5-e914-4710995ca-c5ab0dc85b11"); byte [] bytes_sha1_out = sha1.computehash (bytes_sha1_in); string str_sha1_out = convert. tobase64string (bytes_sha1_out); response. secwebsocketaccept = str_sha1_out;

Content returned by the server:

HTTP/1.1 101 switching protocolsconnection: upgradeserver: Beetle websocket serverupgrade: websocketdate: Mon, 26 Nov 2012 23:42:44 gmtaccess-control-allow-credentials: trueaccess-control-allow-headers: content-typesec-websocket-accept: fckgur8c7osdslfejtwrjw6wo8q =

After the response is processed by the server, the connection handshake is successful and TCP communication can be performed later. websocket sends data after handshaking and is defined by the user like the lower-layer TCP protocol. It still needs to follow the corresponding application protocol specifications... this is also inArticleThere is no reason for convenience directly based on socket TCP.

Data interaction protocol:

This figure is a bit difficult to understand... there are several situations including mask, data length less than 126, less than uint16, less than uint64, and so on. details will be provided later. the entire protocol header consists of three parts: The first part describes the message end situation and type, the second part describes whether there is a mask length, and the third part is the extension length description and mask value.

We can see that the websocket protocol data mainly uses the first two bytes to describe the data packet situation.

First byte

The highest bit is used to describe whether the message ends. If it is 1, the message is the end of the message, and if it is zero, there are subsequent data packets. The last three digits are used to extend the definition, if there is no extension Convention, it must be 0. you can use the following C # code to obtain the corresponding value.

 
Mdatapackage. iseof = (data [start]> 7)> 0;

The minimum 4 bits are used to describe the message type. There are 15 types of messages, of which there are some reserved settings. C # code to get the message type:

 
Int type = data [start] & 0xf; mdatapackage. type = (packagetype) type;

Second byte

The second byte of a message mainly uses a description mask and message length. The maximum bit is 0 or 1 to describe whether there is a mask for processing. You can obtain the value through the following C # code method:

 
Bool hasmask = (data [start]> 7)> 0;

The remaining seven digits are used to describe the message length. Because the seven digits can only describe 127 at most, this value indicates three conditions. One is that the message content is less than 126 of the message length, if the message length is less than uint16, this value is 126. If the message length is greater than uint16, this value is 127; in both cases, the message length is stored in the followed byte [], which is uint16 (2-bit byte) and uint64 (4-bit byte ). you can use the following C # code to obtain the corresponding value.

Mpackagelength = (uint) (data [start] & 0x7f); Start ++; If (mpackagelength = 126) {mpackagelength = bitconverter. touint16 (data, start); Start = start + 2;} else if (mpackagelength = 127) {mpackagelength = bitconverter. touint64 (data, start); Start = start + 8 ;}

If a mask exists, obtain the four-digit mask value:

 
If (hasmask) {mdatapackage. masking_key = new byte [4]; buffer. blockcopy (data, start, mdatapackage. masking_key, 0, 4); Start = start + 4; Count = count-4 ;}

Get Message Body

After obtaining the length of the message body, you can obtain the corresponding length of byte []. Some message types have no length, such as % X8 denotes a connection close. for text messages, the corresponding byte [] is UTF-8 encoded. another thing to note when obtaining the message body is the mask. If a mask exists, the received byte [] Must be converted as follows:

 If (mdatapackage. masking_key! = NULL) {int length = mdatapackage. data. count; For (VAR I = 0; I 
  

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.