1:安装方式: cnpm i reconnecting-websocket
2:引入:import ReconnectingWebSocket from 'reconnecting-websocket';
3:连接websocket
GOD.SOCKET = new ReconnectingWebSocket(wsUrl); GOD.SOCKET.addEventListener('open', () => { GOD.SOCKETTimer = setInterval(() => { if (GOD.SOCKET) { GOD.SOCKET.send('0') } }, 30000); }); GOD.SOCKET.addEventListener('error', (e) => { console.log('WS错误:' + e.code + ' ' + e.reason + ' ' + e.wasClean); const authorization = sessionStorage.getItem('authorization'); if (authorization) { WSLink(); } }); GOD.SOCKET.addEventListener('close', (e) => { console.log('WS断开:' + e.code + ' ' + e.reason + ' ' + e.wasClean); const authorization = sessionStorage.getItem('authorization'); if (authorization) { WSLink(); } }); GOD.SOCKET.addEventListener('message', (socket) => { const result = JSON.parse(socket.data); const {projectId, code, tabsId, type, from, errMsg, timestamp} = result })