当前位置:网站首页>WebSocket的理解以及应用场景
WebSocket的理解以及应用场景
2022-06-25 06:42:00 【沐日(Shrek)】
一、对 WebSocket 的理解
WebSocket是HTML5提供的一种浏览器与服务器进行全双工通讯的网络技术,属于应用层协议。它基于TCP传输协议,并复用HTTP的握手通道。浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接, 并进行双向数据传输。
WebSocket 的出现就解决了半双工通信的弊端。它最大的特点是:服务器可以向客户端主动推动消息,客户端也可以主动向服务器推送消息。
WebSocket原理::客户端向 WebSocket 服务器通知一个带有所有接收者ID的事件,服务器接收后立即通知所有活跃的客户端,只有ID在接收者ID序列中的客户端才会处理这个事件。
WebSocket 特点的如下:
- 支持双向通信,实时性更强
- 可以发送文本,也可以发送二进制数据‘’
- 建立在TCP协议之上,服务端的实现比较容易
- 数据格式比较轻量,性能开销小,通信高效
- 没有同源限制,客户端可以与任意服务器通信
- 协议标识符是ws(如果加密,则为wss),服务器网址就是 URL
- 与 HTTP 协议有着良好的兼容性。默认端口也是80和443,并且握手阶段采用 HTTP 协议,因此握手时不容易屏蔽,能通过各种 HTTP 代理服务器。
二、Websocket的使用方法如下:
// 在index.html中直接写WebSocket,设置服务端的端口号为 9999
let ws = new WebSocket('ws://localhost:9999');
// 在客户端与服务端建立连接后触发
ws.onopen = function() {
console.log("Connection open.");
ws.send('hello');
};
// 在服务端给客户端发来消息的时候触发
ws.onmessage = function(res) {
console.log(res); // 打印的是MessageEvent对象
console.log(res.data); // 打印的是收到的消息
};
// 在客户端与服务端建立关闭后触发
ws.onclose = function(evt) {
console.log("Connection closed.");
};
三、应用场景
基于websocket的事实通信的特点,其存在的应用场景大概有:
- 弹幕
- 媒体聊天
- 协同编辑
- 基于位置的应用
- 体育实况更新
- 股票基金报价实时更新
边栏推荐
- 如何用svn新建属于自己的分支
- 【视频】ffplay 使用mjpeg格式播放usb摄像头
- 搞清信息化是什么,让企业转型升级走上正确的道路
- Keil and Proteus joint commissioning
- VSCode很好,但我以后不会再用了
- Basic use of ActiveMQ in Message Oriented Middleware
- 将数据导入到MATLAB
- 新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
- Vscode is good, but I won't use it again
- @The difference between resource and @autowired annotation, why recommend @resource?
猜你喜欢
STL tutorial 4- input / output stream and object serialization
Requirements for Power PCB circuit board design 2021-11-09
(tool class) use SecureCRT as the communication medium
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
OAuth 2.0一键登录那些事
The fourth floor is originally the fourth floor. Let's have a look
【QT】Qt 5 的程序:打印文档
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
Technology blog | how to communicate using SSE
This article uses pytorch to build Gan model!
随机推荐
Advantages and differences of three kinds of vias in PCB 2021-10-27
Requirements for Power PCB circuit board design 2021-11-09
C reads XML on the web
Five causes of PCB board deformation and six solutions 2021-10-08
基于激光雷达的林业调查常用术语及含义锦集
消息中间件之ActiveMQ的基本使用
How to select lead-free and lead-free tin spraying for PCB? 2021-11-16
27. remove elements
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
Runtime——methods成员变量,cache成员变量
将数据导入到MATLAB
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
@The difference between resource and @autowired annotation, why recommend @resource?
Storage of Galileo broadcast ephemeris in rtklib-b33
Getting started with OpenMP
@Resource和@Autowired注解的不同,为什么推荐@Resource?
WinForm implementation window is always at the top level
Estimation of dense forest volume based on LIDAR point cloud with few ground points
El input to add words to the tail
判断用户是否是第一次进入某个页面