当前位置:网站首页>From simple to deep - websocket
From simple to deep - websocket
2022-06-12 12:33:00 【I'm Mr.C】
Websocket Introduce
Websocket It's a protocol , And http The agreement is similar to , These two protocols are used for communication between client and server .
however ,Websocket And http There is a difference .
That we use a lot http Communication with the server is limited , The server cannot actively send data to the client .
The client sends the request , Server responds to requests , The server cannot actively send data to the customer .
websocket The server can actively communicate with the client 
websocket Use
Just looking at the introduction of theory can not better understand websocket.
websocket The protocol is precisely because the server can actively send information to the client , So it is used more in chat applications .
Of course http Chat applications can also be implemented , That's using polling .
polling —— The client sends requests to the server every once in a while , The client can get the latest data from the server in real time .
1、 establish websocket object ( important , Must be created first websocket object )
let ws=new WebSocket(" Fill in the server-side execution websocket Application path ");// for example :ws://127.0.0.1:8111
// You can see on the agreement that http Differences in requests .
2、 Execute when the client establishes a link with the server ——open event
ws.addEventListener("open",()=>{
console.log(" Link to the server ");
});
3、 Client sends message ——send event
ws.send(" Hello , The server ");// Use send You can send information to the server
4、 The client receives messages sent by the server ——message event
ws.addEventListener("message",(content)=>{
console.log("content It is the message sent from the server :"+content);
})
5、 The client actively closes the link with the server ——close event
ws.close();// The client actively closes the link with the server
6、 After the client closes the link with the server, execute ——close event
ws.addEventListener("close",()=>{
console.log(" The callback is executed after the client closes the link ");
});
7、 Abnormal link between client and server ——error event
ws.addEventListener("error",(err)=>{
console.log(err);
});
error The thing to note about the incident is , When the link is abnormal , Will execute error event , however close Events will also be triggered .
Generalization :
| websocket event | Introduce |
|---|---|
| open | Client and server link |
| send | The client actively sends information to the server |
| message | The client receives messages from the server |
| close | The client actively closes the link with the server |
| close | Execute when the client and server close the link |
| error | Abnormal link between client and server |
Tips:
The server-side small series uses node.js More , So create websocket The application module is called :nodejs-websocket
边栏推荐
猜你喜欢
随机推荐
Boot entry directory
Is yuancosmos a short-term speculation or a future trend?
Stress - system pressure simulation tool
imx6-uboot添加lvds1显示
Tron API wave field transfer query interface PHP version package based on thinkphp5 attached interface document 20220528 version
Bat interview & advanced, get interview materials at the end of the text
Advantages and disadvantages of single page development and multi page development
Suggestions and skills for advanced version of go language test
JS将DOM导出为图片的方法
Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
Bank layout meta universe: digital collections, digital employees become the main track!
Various data transmission
【Leetcode】79. Word search
TRON-api-波场转账查询接口-PHP版本-基于ThinkPHP5封装-附带接口文档-20220528版本
Preliminary study on Regional Simulation of crop model
What is modularity? Benefits of modularity
object. Defineproperty basic usage
Start with Xiaobai, take the weight parameter from the trained model and draw the histogram
Kdd2022 | edge information enhancement graph transformer
Macro compilation preprocessing header Win32_ LEAN_ AND_ MEAN









