关于建设企业网站的请示,指数基金排名前十名,万网代备案系统,长沙服务专业的建网站小程序websocket最佳实践#xff0c;有心跳和断线重连
封装了WebSocketHandler类#xff0c;用于管理websocket链接#xff0c;保证链接的稳定和可靠#xff0c;该类主要适用于小程序#xff0c;但其设计思想和方法也适用于其他平台。
export default class WebSocketHa…小程序websocket最佳实践有心跳和断线重连
封装了WebSocketHandler类用于管理websocket链接保证链接的稳定和可靠该类主要适用于小程序但其设计思想和方法也适用于其他平台。
export default class WebSocketHandler {constructor() {// 获取环境变量this.env wx.getStorageSync(env);// 生产环境的 WebSocket 地址this.prod www.xx.com;// 开发环境的 WebSocket 地址this.dev www.xx.com;// 根据环境变量选择 WebSocket 地址this.path ${(this.env pro || !this.env) ? this.prod : this.dev};// 构建 WebSocket URLthis.url wss://${this.path}/websocket;// 重连次数限制this.limit 0;// WebSocket 连接状态标志this.isClose true;// 存储未发送的消息this.preParams ;// 重连锁this.lockReconnect false;// 重连定时器this.timer null;// 心跳检测机制this.heartCheck {timeout: 3000, // 心跳检测间隔timeoutObj: null, // 客户端定时器serverTimeoutObj: null, // 服务端定时器reset: function () {// 重置定时器clearTimeout(this.timeoutObj);clearTimeout(this.serverTimeoutObj);return this;},start: () {// 启动心跳检测this.heartCheck.timeoutObj setTimeout(() {// 发送 PING 消息wx.sendSocketMessage({data: PING,success() {// 发送成功}});// 设置服务端定时器如果超时则关闭连接this.heartCheck.serverTimeoutObj setTimeout(() {wx.closeSocket();}, this.heartCheck.timeout);}, this.heartCheck.timeout);}};// 绑定 WebSocket 事件处理函数this._onSocketOpen();this._onSocketMessage();this._onSocketError();this._onSocketClose();}/*** 创建 WebSocket 连接* param {Object} options - 建立连接时需要的配置信息* param {string} options.userId - 用户 ID* param {string} options.agentId - 代理 ID*/connect({ userId}) {if (!this.isClose) {logger.info(WebSocket is already connected or connecting.);return;}wx.connectSocket({url: ${this.url}/${userId},header: {content-type: application/json},method: POST});}/*** 发送消息* param {Object} params - 要发送的消息参数* returns {Promise} - 返回一个 Promise表示消息发送的结果*/send(params) {return new Promise((resolve, reject) {const cruParams JSON.stringify(params);if (this.isClose) {this.preParams params;return;}logger.debug(send---, JSON.stringify(cruParams));wx.sendSocketMessage({data: cruParams,success: (res) {logger.debug(sendSocketMessage, JSON.stringify(res));resolve({ res });},fail: (res) {logger.error(sendSocketMessage, JSON.stringify(res));reject(res);},});});}/*** 关闭 WebSocket 连接*/closeConnection() {this.isClose true;wx.closeSocket();}/*** 处理 WebSocket 错误事件*/_onSocketError() {wx.onSocketError((res) {this.isClose true;logger.error(WebSocket连接打开失败请检查, JSON.stringify(res));this.reconnect();});}/*** 处理 WebSocket 关闭事件*/_onSocketClose() {wx.onSocketClose((res) {this.isClose true;logger.error(WebSocket 已关闭, JSON.stringify(res));this.reconnect();});}/*** 处理 WebSocket 打开事件*/_onSocketOpen() {wx.onSocketOpen(() {logger.debug(WebSocket连接已打开);this.heartCheck.reset().start();if (this.isClose this.preParams) {logger.info(重新发送WebSocket消息);wx.sendSocketMessage({data: JSON.stringify(this.preParams),success: () {logger.debug(重新发送--sendSocketMessage, JSON.stringify(this.preParams));this.preParams ;},fail: (res) {logger.error(重新发送--sendSocketMessage-fail, JSON.stringify(res));},});} else {wx.sendSocketMessage({data: PING,});}this.isClose false;});}/*** 处理 WebSocket 消息事件*/_onSocketMessage() {wx.onSocketMessage((res) {if (res.data PONG) {console.log(PONG成功);this.heartCheck.reset().start();}});}/*** 尝试重新连接 WebSocket*/reconnect() {if (this.lockReconnect) return;this.lockReconnect true;clearTimeout(this.timer);if (this.limit 50) {this.timer setTimeout(() {this.lockReconnect false;}, 300);this.limit this.limit 1;}}
}
// 使用时
this.wsHandler new WebSocketHandler();
app.wsHandler.send(params).then();
文章转载自: http://www.morning.rchsr.cn.gov.cn.rchsr.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.rqlf.cn.gov.cn.rqlf.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.rswfj.cn.gov.cn.rswfj.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.hpdpp.cn.gov.cn.hpdpp.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.fmrrr.cn.gov.cn.fmrrr.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.snmth.cn.gov.cn.snmth.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.wdhhz.cn.gov.cn.wdhhz.cn http://www.morning.cgdyx.cn.gov.cn.cgdyx.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.gprzp.cn.gov.cn.gprzp.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn