Analysis of WebSocket protocol implementation in HTML5, html5websocket

Source: Internet
Author: User

Analysis of WebSocket protocol implementation in HTML5, html5websocket
This article mainly introduces the principles of implementing the WebSocket protocol in html5. it helps beginners understand the WebSocket protocol. For more information, see

The purpose of the WebSocket protocol is to work on the existing network infrastructure. As part of this design principle, the Protocol Specification for WebSocket connection defines an HTTP connection as its initial lifecycle to ensure its full backward compatibility with the pre-WebSocket world. Generally, switching from HTTP to WebSocket is called WebSocket handshake.

The browser sends a request to the server, which indicates that it wants to switch from the HTTP WebSocket protocol. The client expresses its wish by upgrading the title. The Code is as follows:


The Code is as follows: GET ws: // echo.websocket.org /? Encoding = text https/1.1
Origin: http://websocket.org
Cookie: _ utma = 99as
Connection: Upgrade
Host: echo.websocket.org
Sec-WebSocket-Key: uRovscZjNol/umbTt5uKmw =
Upgrade: websocket
Sec-WebSocket-Version: 13


If the server understands the WebSocket protocol, it agrees to protocol switching through the upgrade header.


The Code is as follows: HTTP/1.1 101 WebSocket Protocol Handshake
Date: Fri, 10 Feb 2012 17:38:18 GMT
Connection: Upgrade Server: Kaazing Gateway
Upgrade: WebSocket
Access-Control-Allow-Origin: http://websocket.org
Access-Control-Allow-Credentials: true
Sec-WebSocket-Accept: rLHCkw/SKsO9GAH/ZSFhBATDKrU =
Access-Control-Allow-Headers: content-type


At this time, the HTTP connection is decomposed and replaced by the WebSocket connection in the same underlying TCP/IP connection. The WebSocket connection uses the same HTTP (80) as the HTTPS port (443). By default.

After the connection is established, the WebSocket data frame can send back and forth between the client and the server in full duplex mode. Two types of text and binary frames can be sent simultaneously in two directions. The minimum frame value is two bytes. For text frames, each frame starts from 0 x00 bytes and ends with 0 xff bytes, including utf-8 data. The WebSocket text framework uses a Terminator, while the binary frame uses the prefix length.


HTML5 webSocket

Is a set of completed protocols. It is very simple to call new WebSocket (url) to shake hands with the server, WebSocket. send () to send messages, and receive messages from onmessage event processing.
Appendix: the server is not easy to do. For details, refer to Torando (a python-based server that supports websocket)

C # Can the WinFormsApplication socket communicate with the html5 webSocket?

Of course, you can communicate. html5 webSocket runs the WebSocket protocol in html5. That is to say, when C #'s WinFormsApplication socket communicates with html5 webSocket, you must also follow the WebSocket protocol in html5. The common point is that some data identifies what you want to do when sending and receiving data. These identifiers are defined in this Protocol. If your WinFormsApplication socket does not follow this protocol,
Html5 webSocket may not bird you, or tell you that your requirements are wrong.
You can check the webSocket protocol www.cnblogs.com/...3.html
Songpengfei.iteye.com/blog/1178310

This is generally the case

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.