import{ mitts }from"./tool";/* * @Author: lzx * @Date: 2022-05-25 15:42:37 * @LastEditors: lzx * @LastEditTime: 2022-08-18 15:01:38 * @Description: Fuck Bug * @FilePath: \talk_pc\src\utils\socket.ts */let socketUrl: any ="";// socket地址let websocket: any =null;// websocket 实例let heartTime: any =null;// 心跳定时器实例let socketHeart: number =0;// 心跳次数letHeartTimeOut: number =3000;// 心跳超时时间let socketError: number =0;// 错误次数// 初始化socketconstinitWebSocket=(url: any)=>{ socketUrl = url; // 初始化 websocket websocket =newWebSocket(url); websocketonopen(); websocketonmessage(); sendSocketHeart()};// socket 连接成功constwebsocketonopen=()=>{ websocket.onopen=function(e: any){ console.log("连接 websocket 成功", e); resetHeart(); };};// socket 连接失败constwebsocketonerror=()=>{ websocket.onerror=function(e: any){ console.log("连接 websocket 失败", e); };};// socket 断开链接constwebsocketclose=()=>{ websocket.onclose=function(e: any){ console.log("断开连接", e); };};// socket 接收数据constwebsocketonmessage=()=>{ websocket.onmessage=function(e: any){ let msg =JSON.parse(e.data); if(msg.type==='heartbeat'){ resetHeart() console.log("心跳"); } // console.log("收到socket消息", JSON.parse(e.data)); test(msg)// 测试数据 };};// socket 发送数据constsendMsg=(data: any)=>{ websocket.send(data);};// socket 错误constwebsocketError=()=>{ websocket.onerror=function(e: any){ console.log("socket 错误", e); };};// socket 重置心跳constresetHeart=()=>{ socketHeart =0; socketError =0; clearInterval(heartTime); sendSocketHeart();};// socket心跳发送constsendSocketHeart=()=>{ heartTime =setInterval(()=>{ if(socketHeart <=2){ console.log("心跳发送:", socketHeart); websocket.send( JSON.stringify({ content:"", requestId:"aa9872be-d5b9-478e-aba4-50527cd3ef32", type:"heartbeat" }) ); socketHeart = socketHeart +1; }else{ reconnect() } },HeartTimeOut);};// socket重连constreconnect=()=>{ if(socketError <=2){ clearInterval(heartTime); initWebSocket(socketUrl); socketError = socketError +1; console.log("socket重连", socketError); }else{ console.log("重试次数已用完的逻辑", socketError); clearInterval(heartTime); }};// 测试收到消息传递consttest=(msg: any)=>{ switch(msg.type){ case'heartbeat'://加入会议 mitts.emit('heartbeat', msg) break; }}export{ initWebSocket, websocketonmessage, sendMsg, websocketonopen, websocketonerror, websocketclose, websocketError, resetHeart, sendSocketHeart, reconnect,};