1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| import { useStore } from "@/store/index"; import { postMessageToParent } from "./common"; let webSocket: any = null; let timeout: any = null; let reTimes = 0; export let socketOpen: boolean;
const store = useStore();
export const outputWebSocket = (data: any) => { console.log(data); postMessageToParent(data); if (typeof window.webFunctionHandler == "function") { webFunctionHandler(data); } };
export const contactSocket = () => { webSocket.onopen = function () { heartCheck.reset().start(); const outputStr = JSON.stringify({ type: "onOpen", data: null }); outputWebSocket(outputStr); store.changeWebSocketStatus(true); ElMessage({ type: "success", message: "连接成功", }); }; webSocket.onmessage = function (evt: { data: any }) { heartCheck.reset().start(); const received_msg = evt.data; if (!received_msg.includes("HEARTBEAT")) { const parseData = JSON.parse(evt.data); if (store.openId !== parseData.sendOpenId) { const outputStr = JSON.stringify({ type: "onMessage", data: JSON.parse(received_msg), }); store.changeNewMsg(parseData); if (parseData.msgContent === "rocket-[抖动]") { postMessageToParent("shakeOnce"); } else { outputWebSocket(outputStr); } } if (!store.curListId.includes(parseData.roomId)) { store.changeNewMsg({ ...parseData, messageTypeNote: "newRoom" }); } } }; webSocket.onclose = function () { const outputStr = JSON.stringify({ type: "onClose", data: null }); outputWebSocket(outputStr); ElMessage({ type: "info", message: "连接断开,重连中,请耐心等待...", }); reconnect(); }; webSocket.onerror = function () { const outputStr = JSON.stringify({ type: "onError", data: null }); outputWebSocket(outputStr); ElMessage({ type: "info", message: "连接异常,重连中,请耐心等待...", }); reconnect(); }; };
export const initWebSocket = () => { const url = `wss://rocket-chat.nbhuojian.com/webSocket/101/web/${store.openId}`; if ("WebSocket" in window) { webSocket = new WebSocket(url); } contactSocket(); };
const reconnect = () => { clearTimeout(timeout); if (socketOpen) { reTimes = 0; return; } reTimes++; if (reTimes > 3) { ElMessage({ type: "error", message: "连接已断开,请联系管理员", }); store.changeWebSocketStatus(false); outputWebSocket("重连失败"); return; } socketOpen = true; timeout = setTimeout(function () { initWebSocket(); socketOpen = false; }, 2000); };
window.onbeforeunload = function () { const store = useStore(); if (store.isDownLoad) { store.changeIsDownLoad(false); return; } webSocket.close(); };
const heartCheck = { timeout: 3000, timeoutObj: null, serverTimeoutObj: null, reset: function () { this.timeoutObj && clearTimeout(this.timeoutObj); this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj); return this; }, start: function () { this.timeoutObj = setTimeout(() => { const pingStr = JSON.stringify({ msgType: "HEARTBEAT", }); if (webSocket.readyState === 1) { webSocket.send(pingStr); } this.serverTimeoutObj = setTimeout(function () { store.changeWebSocketStatus(false); outputWebSocket("重连失败"); webSocket.close(); }, this.timeout) as any; }, this.timeout) as any; }, };
|