WebSocket--How it works

Source: Internet
Author: User

1, Resume socket connection, start server
2, the client writes the data inside the HTML to establish the connection
<script type= "Text/javascript" >
var socket = new WebSocket ("Ws://127.0.0.1:8002/xxoo");
...
</script>

3, both sides build handshake
The server gets the data that the client sent over
Get the Sec-websocket-key from the head.
Take magic_string and Sec-websocket-key for HMAC1 encryption, and then Base64 encryption
Return the encrypted results to the client

4 If his encrypted results are returned to the client
If the same as the client, continue the connection,
If it is not the same, the client disconnects from the server

5, the client and the service side of the transfer of data, the need for packet-and-package process, the client's JavaScript has encapsulated package and package process,
But the socket server needs to be implemented manually.

6, the process of unpacking is divided into 3 steps: Ps:payload is an identity, telling the program to do it to me, followed by something else, like masking key
And the content of the data
1, if the length of payload is less than 125, the data behind
2, if payload length is equal to 126, then 16 bits is masking key, after masking key is the data
3, if payload length is equal to 127, then the 64 bits behind it is masking key, which is the data behind masking key.

7. Packets are also required when the server sends data to the client.

WebSocket--How it works

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.