当前位置:网站首页>websocket心跳机制(保活机制)
websocket心跳机制(保活机制)
2022-07-28 22:04:00 【仙女爱吃鱼】
原理:使⽤setInterval和setTimeout实现,每隔⼀段时间就向服务器发送⼀个数据包,告诉服务器⾃⼰还活着,如果服务器也
活着就回传⼀个数据包,如果断开,需要客户端重连
1.Websocket连接成功时创建⼼跳检测
socket.open() {
heart()
}
2.Websocket接收消息后进⾏⼼跳重置
heart()
3.使⽤setInterVal和setTimeout⽅法实现⼼跳
data() {
return {
timeoutObj: null,
serverTimeoutObj: null
}
}
heart() {
// 1.通过关闭定时器和倒计时进⾏重置⼼跳
clearInterVal(timeoutObj)
clearTimeout(serverTimeoutObj)
// 2.每隔30秒向服务端发送⼀个数据包
this.timeoutObj = setInterVal(() => {
console.log(‘重置检测⼼跳’)
const data = {}
socket.send(data) // 发送数据包
// 2秒之后 服务端没有返回数据包就关闭连接重连
serverTimeoutObj = setTimeout(() => {
console.log(‘⼼跳没有了,连接断掉了’)
socket.close()
}, 2000)
}, 3000)
}
边栏推荐
- 解决:Direct local .aar file dependencies are not supported when building an AAR.
- [self] - question brushing - dynamic programming
- Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (2. Compatibility of kingbasees v8.3 and v8.6)
- The computer doesn't know what to uninstall, can't open the calculator, can't edit screenshots, can't open txt files, and so on
- Jincang database kingbasees client programming interface guide ODBC (5. Development process)
- Deep analysis of integrated learning gbdt
- leetcode 763. Partition Labels 划分字母区间(中等)
- 电商数据模型设计
- 2022年R2移动式压力容器充装考题模拟考试平台操作
- sql 左连接,内连接 的写法「建议收藏」
猜你喜欢
Xss.haozi.me range details
How powerful can top "hackers" be? Internet access without signal, expert: high-end operation!
尿酸酶丨Worthington猪肝尿酸酶的特征:
解决:Direct local .aar file dependencies are not supported when building an AAR.
电商数据模型设计
2022 R2 mobile pressure vessel filling test question simulation test platform operation
MySQL introduction
Codeforces Round #810 (Div. 2) A - C
YOLOV5学习笔记(一)——原理概述
Okaleido生态核心权益OKA,尽在聚变Mining模式
随机推荐
hutool官网(hutool好用吗)
剑指 Offer 64. 求1+2+…+n,逻辑运算符短路效应
Pagoda phpMyAdmin unauthorized access vulnerability
Optimization and implementation of custom MVC
Inspur clusterenginev4.0 remote command execution vulnerability cve-2020-21224
刨根问底学 二叉树
毕业三年之际写给可能迷茫的你我[转]
多传感器融合定位(三)——惯性技术
2022 R2 mobile pressure vessel filling test question simulation test platform operation
SAP 临时表空间错误处理
商家对积分体系运营的两个误解
简述分组密码的加密分组链接模式的工作原理及其特点(密码学移位密码加密解密)
Worthington核糖核酸测定详细攻略
Kingbasees client programming interface guide ODBC (4. Create data source)
Zabbix 5.0 使用自带Redis模版监控
浪潮ClusterEngineV4.0 远程命令执行漏洞 CVE-2020-21224
深度剖析集成学习Xgboost(续)
2022 welder (Junior) work license questions and answers
The computer doesn't know what to uninstall, can't open the calculator, can't edit screenshots, can't open txt files, and so on
2022年R2移动式压力容器充装考题模拟考试平台操作