Websocket Protocol resolution

Source: Internet
Author: User
Tags chr

WebSocket protocol is a new protocol for HTML5. It is the implementation of browser-to-server full-duplex communication (Full-duplex).

  Many web sites are now polling (polling) for instant messaging. Polling is at a specific time interval (such as every 1 seconds), the browser sends an HTTP request to the server, and the server returns the latest data to the client's browser. This traditional HTTP request pattern has obvious drawbacks – 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 the most new technology to do polling effect is comet– using AJAX. However, although this technology can reach full-duplex communication, it still needs to make a request.    in the WebSocket API, browsers and servers only need to do a handshake, and then a fast channel is formed between the browser and the server. Between the two directly can transfer data to each other, changed the original B/S mode.  

here is the implementation of WebSocket in PHP, this article, I mainly on the WebSocket protocol to a simple introduction.

Websocket business model

Browser-side WebSocket requests are generally

Javacsript

var ws = new WebSocket ("ws://127.0.0.1:4000");

Ws.onopen = function () {

Console.log ("succeed");

};

Ws.onerror = function () {

Console.log ("error");

};

Ws.onmessage = function (e) {

Console.log (e);

}

When new is a WebSocket object, a GET request is sent to the server

this request is to touch a server port, in general, the server will pre-bind a socket to a port, the client and server side on this predetermined port communication (I am bound here is 4000 port, by default, Websocke use 80 port).

then, on the server side of the socket to hear this packet after the generation of a new socket, the data sent over the Sec-websocket-key parse out, and then follow the "Sec-websocket-ke" plus a magic string "258eafa5-e914-47da-95ca-c5ab0dc85b11". Use SHA-1 encryption, then BASE-64 encode the result as the "sec-websocket-accept" header value, returned to the client.

after the client receives this, the communication protocol is upgrade to the WebSocket protocol.

It then communicates under this persistent channel, including browser queries, server push, and the two parties communicating with each other in a full duplex state. The format of the data transfer frame in the Switched WebSocket protocol (no longer using the HTML protocol at this time) is given in the official documentation:

look at this directly, who will have a nod big: I spent a picture to simply explain the structure of this frame.

Explanation of each field:

FIN 1bit represents the last frame of information, flag, which is the marker

RSV 1-3 1bit Each after the standby default is 0

Opcode 4bit Frame type,

Mask 1bit masks, whether data is encrypted, default must be set to 1

Payload len 7bit length of data, when this 7 bit data = = 126, the following 2 bytes is also the data length, when it = = 127, the following 8 bytes represents the data length Masking-key 1 or 4 bit Mask payload Data playload len bytes

so our code here, by judging the value of Playload Len, uses substr to intercept Masking-key and Playloaddata.

The methods for parsing data based on masks are:

for (i = 0; i < data.length; i++) {

Orginaldata + = data[i] ^ maskingkey[i MoD 4];

}

in PHP, when we receive the data, we intercept the data according to the format here, and then we parse it in this way to get the data sent by the browser. When we want to send the data to the browser, we also need to assemble the frame in this format. Here is my method:

function frame ($s) {

$a = str_split ($s,);

if (count ($a) = = 1) {

return "\x81". Chr (strlen ($a [0]). $a [0];

}

$ns = "";

foreach ($a as $o) {

$ns. = "\x81". Chr (strlen ($o)). $o;

}

return $ns;

}

force the data to be sent into a byte/frame, so that playload Len requires only 7 bits. That is, directly ascall the length of the data into the code, and then follow the data to be sent. The ' \x81 ' in front of each frame is used in binary: 0001 :

1 is FIN.

000 is a three backup bit.

0001: Refers to the official explanation of opcode:

you can set the value of the opcode to tell the browser the data property of the frame.

Websocket Protocol resolution

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.