WebSocket Basic Concepts

Source: Internet
Author: User

(1) WebSocket Protocol and HTTP protocol

websocket 协议,双工通道  socket连接不断开http 协议 ,只能请求响应 (用户主动获取)   socket连接断开都是基于socket            ---- 客户端都是浏览器

(2) polling

    一般用户量少的 都是用 websocket来做即时通讯轮询 ---  一直发请求,定死的,2秒钟取一次长轮询 --- 请求之后,没有新消息,夯住,30秒之内没有新消息,断开            重新请求,如果有新消息,立刻返回消息

(3) WS Protocol Small case

创建 socket 服务端        import socket        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)        sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)        sock.bind(('127.0.0.1',8002))        sock.listen(5)        conn,addr = sock.accept()        data = conn.recv(1024)        print(data) 创建 scoket 客户端     <script type="text/javascript">            var socket = new WebSocket("ws://127.0.0.1:8002")    //        浏览器 会立即创建一个 socket对象 ,连接        </script>    </body>但是服务端 必须返回固定格式的内容给浏览器 ,才能保证 正常的连接【握手信息】魔法字符串

(5) The WebSocket protocol is a new protocol based on TCP.

WebSocket was originally referred to as a TCP connection in the HTML5 specification as a placeholder for the TCP-based socket API.
It implements browser-to-server full-duplex (Full-duplex) communication.
The essence is to keep the TCP connection and communicate through the socket on the browser and server.

请求和响应的【握手】信息需要遵循规则:    从请求【握手】信息中提取 Sec-WebSocket-Key    利用magic_string 和 Sec-WebSocket-Key 进行hmac1加密,再进行base64加密    将加密结果响应给客户端注:magic string为:258EAFA5-E914-47DA-95CA-C5AB0DC85B11

(6) Tornado realization WebSocket

Tornado是一个支持WebSocket的优秀框架,当然Tornado内部封装功能更加完整。

(7) When transmitting data between client and server, "packet" and "unpack" data are required.

客户端的JavaScript类库已经封装【封包】和【解包】过程,但Socket服务端需要手动实现。第一步:获取客户端发送的数据【解包】 Payload第二步:向客户端发送数据【封包】

WebSocket Basic Concepts

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.