当前位置:网站首页>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)
}
边栏推荐
- EN 1935建筑五金.单轴铰链—CE认证
- 利用递归和链表头插法实现链表成组的翻转——LeetCode25 K个一组翻转链表
- Custom MVC principle and framework
- 2022 G2 power plant boiler stoker examination question bank simulated examination platform operation
- Xss.haozi.me range details
- RHCE the next day
- Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (3. Kingbasees migration capability support system)
- Hyperparametric optimization (grid search and Bayesian Optimization)
- 简述分组密码的加密分组链接模式的工作原理及其特点(密码学移位密码加密解密)
- Worthington核糖核酸测定详细攻略
猜你喜欢

电商数据模型设计

电脑不知卸载什么,打不开计算器无法编辑截图功能打不开txt文件等等解决方案之一

Equipped with a new generation of ultra safe cellular batteries, Sihao aipao is available from 139900 yuan

Deep analysis of integrated learning xgboost

【自】-刷题-峰值

【自】-刷题-字符串

Pagoda phpMyAdmin unauthorized access vulnerability

MySQL transaction and storage system

Deep analysis of integrated learning AdaBoost

Worthington核糖核酸酶B历史和化学性质说明
随机推荐
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
零视科技 H5S视频平台 GetUserInfo 信息泄漏漏洞 CNVD-2020-67113
leetcode 763. Partition Labels 划分字母区间(中等)
EN 12101-8:2011烟雾和热量控制系统防烟挡板—CE认证
Codeforces Round #810 (Div. 2) A - C
Compatibility description between kingbasees and Oracle (3. Common functions)
(22杭电多校二)Two Permutation (dp),Package Delivery (贪心)
Worthington核糖核酸酶B历史和化学性质说明
Blocking queue
请简述list,set,map类型的集合的各自特点(简述三种不同的继承方式下)
Mongodb index add, view, export, delete
2022 R2 mobile pressure vessel filling test question simulation test platform operation
xss.haozi.me靶场详解
深度剖析集成学习GBDT
Samba service setup
迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
Optimization and implementation of custom MVC
深度之眼(十八)——偏导数
从XSS Payload学习浏览器解码
超参数优化(网格搜索和贝叶斯优化)