当前位置:网站首页>Websocket of Web real-time communication technology
Websocket of Web real-time communication technology
2022-07-02 18:42:00 【Hua Weiyun】
“ The article is right Websocket Introduced and summarized ( contain python Programming code ).”
Lead to read
- Why introduce Websocket?
- —— Because in HTTP In , The server cannot send a message to the client Take the initiative to push Updated resources
- PS:HTTP/2 Only static resources can be pushed ( Preset resources ), Unable to push real-time updated information
- HTTP—— Connection is established - Client request - Server response ( If there is no request, the server will not respond )
- Websocket—— Implement client - Server two-way peer-to-peer communication ( Both the server and the client can actively send messages )
- Full duplex protocol ( full duplex —— Communication data can be transmitted in both directions at the same time , Half duplex —— Communication data can only be transmitted in one direction at the same time , Simplex —— Communication data can only be transmitted in one direction )
- Websocket—— Full duplex protocol
- Typical applications : Barrage system
Websocket: The host port is occupied for a long time
HTTP request : Listen to the port and transmit information through the port
- Tradition HTTP How to update information :
polling / A long connection
Many websites are trying to implement push technology , The technology used is polling . Polling is done at specific intervals ( As per 1 second ), Issued by the browser to the server HTTP request , The server then returns the latest data to the client's browser . This traditional model brings obvious disadvantages , That is, the browser needs to keep making requests to the server , However HTTP Requests may contain long headers , The amount of data that actually works is probably only a tiny fraction of that , Obviously this will waste a lot of bandwidth and other resources .
Websocket Introduce
Basics
Websocket And HTTP and HTTPS Use the same TCP port , You can bypass most firewalls . By default ,Websocket Agreement to use 80 port ; Running on the TLS When above , By default 443 port .
establish WebSocket Connect
The client browser sends a message to the server HTTP request ( Request containing updated header information )
Server side parsing —— An answer message is generated to indicate that the upgrade is successful
Connection established successfully —— End to end communication —— Until a party initiates a shutdown request
In the handshake ,Websocket Than HTTP One more handshake , Update the information ( Server response status code 101 Express ), As shown in the figure below
Client message :
- Server message :
- Connection You have to set Upgrade, Indicates that the client wants to upgrade the connection .
- Upgrade Field must be set Websocket, I want to upgrade to Websocket agreement .
Origin: For safe use , Prevent cross station attacks , Browsers generally use this to identify the original domain .
Python Websocket build
Server setup code :
- Client build code :
The client connects to the server , After the server accepts the connection, both parties establish an end-to-end connection TCP Connect , Two way communication can be realized on this connection , After establishing this connection , There is no distinction between client and server between the two sides of communication , What is provided is End-to-end communication .
Websocket VS HTTP
- Websocket Cannot connect through proxy , Must be connected directly
- Websocket After establishment , Both sides of communication can actively communicate at any time
- Websocket After establishment, information is transmitted through data frames , Do not use request - Response structure
- Websocket Data frames are in order
advantage
- Less control overhead . After the connection is established , When data is exchanged between server and client , The packet header used for protocol control is relatively small . Without the extension , For server to client content , This head is only the size of 2 to 10 byte ( Related to packet length ); For client to server content , This head also needs to be added with extra 4 Byte relative to HTTP Request to carry the full head every time , This cost significantly reduces .
- Strong real time : Because the protocol is full duplex , So the server can send data to the client at any time . be relative to HTTP The request needs to wait for the client to send the request to the server to respond , Delays are significantly less ; Even with Comet And so on Compare , It can also transfer data more than once in a short time .
- Better binary support .Websocket Defined binary frame , relative HTTP, Binary content can be handled more easily .
- Support extended protocols .Websocket Defined extensions , The user can extend the protocol 、 Implement some custom sub protocols . For example, some browsers support compression .
- Stateful protocol ( contrast HTTP No state ). And HTTP Different ,Websocket You need to establish a connection first , After that, some state information can be omitted during communication . and HTTP The request may need to carry status information in each request ( Such as cookies etc. ).
- WebSocket Not limited by browser homology policy
- WebSockets It is suitable for applications with high real-time requirements 、 The chat room 、 Multiplayer game 、 Real time document collaboration, etc
shortcoming
- The long-term maintenance of the server requires high costs
- Under the high concurrency scenario, the requirements for both sides of communication are high
- It occupies multiple process ports and is limited by general browsers ( for example :firefox by 200)
- WebSocket The handshake phase is prone to security problems (Hijacking)
Reference material
“ The scars and marks of chasing dreams are the unique medals of teenagers , Long winds and waves will eventually , Enjoy the process , Bitterness and bitterness coexist , It's life .”
——Created By It's feather 18 ya
边栏推荐
- RTE11- 中断解耦功能
- Basic idea of quick sorting (easy to understand + examples) "suggestions collection"
- Wechat nucleic acid detection appointment applet system graduation design (2) applet function
- Responses of different people in technology companies to bugs | daily anecdotes
- Esp32-c3 introductory tutorial question ⑪ - ESP tls: create_ ssl_ handle failed, tls_ io_ instance->options. trusted_ certs null
- Leetcode 面试题 17.04. 消失的数字
- @Component 拿不到dao层
- Esp32-c3 introductory tutorial question ⑩ - error: implicit declaration of function 'ESP_ blufi_ close‘;
- Unity learning shader notes [81] simple color adjustment post-processing (brightness, saturation, contrast)
- Ue4 dessine un cercle avec une ligne de contour
猜你喜欢
Leetcode 面试题 17.01. 不用加号的加法
Leetcode 面试题 16.15. 珠玑妙算
Web版3D可视化工具,程序员应该知道的97件事,AI前沿论文 | 资讯日报 #2022.07.01
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
什么是云原生?这回终于能搞明白了!
工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
NM01-独立于总线协议的NM模块功能概述与API定义
Babbitt | metauniverse daily must read: can you buy a virtual anchor for 1000 yuan? Is this the live gospel of small businesses or "cutting leeks"
Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用
Redis (6) -- object and data structure
随机推荐
Pychar modify pep8 e501 line too long > 0 characters
“栈”的典型应用—表达式求值(C语言实现)
300+篇文献!一文详解基于Transformer的多模态学习最新进展
Détends - toi encore! Ces nouveaux étudiants peuvent s'installer directement à Shanghai
Wechat applet video sharing platform system graduation design (3) background function
Ali was wildly asked by the interviewer on three sides. Redis dared not write 'proficient' on his resume anymore
如何优雅的写 Controller 层代码?
Three methods of MySQL backup
StretchDIBits函数
Server PHP environment building tutorial, PHP server environment building graphic explanation
Leetcode interview question 17.01 Addition without plus sign
[Northwestern Polytechnic University] information sharing of the first and second postgraduate examinations
How to use PS to extract image color and analyze color matching
options should NOT have additional properties
A simple PHP personal card issuing program v4.0
Relax again! These fresh students can settle directly in Shanghai
【西北工业大学】考研初试复试资料分享
Nm02 nm module call sequence diagram and code interpretation independent of bus protocol
In early summer, Kaiyuan magic changed an electric mosquito racket with killing sound effect!
ESP32-C3入门教程 问题篇⑩——error: implicit declaration of function ‘esp_blufi_close‘;