当前位置:网站首页>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
边栏推荐
- Responses of different people in technology companies to bugs | daily anecdotes
- 300+篇文献!一文详解基于Transformer的多模态学习最新进展
- cJSON 使用详解
- Use dosbox to run the assembly super detailed step "suggestions collection"
- Wechat applet video sharing platform system graduation design completion (8) graduation design thesis template
- 1.5.1版本官方docker镜像运行容器,能设置使用 mysql 8驱动吗?
- Iframe nesting details
- NVIDIA graphics card failed to initialize nvml driver/library version mismatch error solution
- Leetcode(81)——搜索旋转排序数组 II
- Steamos 3.3 beta release, steam deck Chinese keyboard finally came
猜你喜欢
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
Ue4 dessine un cercle avec une ligne de contour
Redis (6) -- object and data structure
科技公司不同人对Bug的反应 | 每日趣闻
夜神模擬器+Fiddler抓包測試App
Implementation shadow introduction
RDK仿真实验
Leetcode 面试题 16.15. 珠玑妙算
Leetcode interview question 16.17 Continuous sequence
Nm01 function overview and API definition of nm module independent of bus protocol
随机推荐
719. Find the distance of the number pair with the smallest K
Esp32-c3 introductory tutorial question ⑪ - ESP tls: create_ ssl_ handle failed, tls_ io_ instance->options. trusted_ certs null
Deep neural network Summary
Leetcode(154)——寻找旋转排序数组中的最小值 II
QQmlApplicationEngine
C # detect whether the picture is rotated and modified to the true rotation
服务器php环境搭建教程,PHP服务端环境搭建图文详解
Export Excel files using npoi
MySQL 关于 only_full_group_by 限制
又一所双非改考408,会爆冷么?南昌航空大学软件学院
Steamos 3.3 beta release, steam deck Chinese keyboard finally came
C语言中函数参数传递的三种方式
饭卡 HDU2546
Typical application of "stack" - expression evaluation (implemented in C language)
Meal card hdu2546
Leetcode 面试题 17.04. 消失的数字
How to set vscode to delete the whole line shortcut key?
ESP32-C3入门教程 问题篇⑩——error: implicit declaration of function ‘esp_blufi_close‘;
Leetcode 面试题 17.01. 不用加号的加法
Wechat applet video sharing platform system graduation design completion (4) opening report