1 line
5.4 KiB
JSON
1 line
5.4 KiB
JSON
|
|
{"ast":null,"code":"class WebSocketClient {\n constructor(url) {\n this._url = url;\n this._callbacks = {};\n this._reconnectTimer = null;\n this.init();\n }\n init() {\n this._ws = new WebSocket(this._url);\n this._ws.onopen = () => {\n this.onopen();\n };\n this._ws.onmessage = e => {\n this.onmessage(e);\n };\n this._ws.onclose = e => {\n if (e.code != 1000) {\n // 状态码:1000 正常关闭,不重新建立连接\n this.onclose(e);\n // 尝试重连\n this.reconnect();\n }\n };\n }\n onopen() {\n console.log('WebSocket open: ', this._url);\n }\n onmessage(e) {\n console.log('WebSocket message:', e.data);\n if (e.data) {\n const data = JSON.parse(e.data);\n const type = 'message';\n if (type in this._callbacks) {\n this._callbacks[type](data);\n }\n }\n }\n onclose(e) {\n console.log('WebSocket close:', e);\n }\n reconnect() {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = setTimeout(() => {\n console.log('WebSocket reconnect:', this._url);\n this.init();\n }, 5000);\n }\n send(data) {\n if (this._ws.readyState === WebSocket.OPEN) {\n console.log('WebSocket send:', JSON.stringify(data));\n this._ws.send(JSON.stringify(data));\n }\n }\n registerCallback(type, callback) {\n this._callbacks[type] = callback;\n }\n}\nexport default WebSocketClient;","map":{"version":3,"names":["WebSocketClient","constructor","url","_url","_callbacks","_reconnectTimer","init","_ws","WebSocket","onopen","onmessage","e","onclose","code","reconnect","console","log","data","JSON","parse","type","clearTimeout","setTimeout","send","readyState","OPEN","stringify","registerCallback","callback"],"sources":["/mnt/sdc/Project1/main/client/src/utils/websocket.js"],"sourcesContent":["class WebSocketClient {\r\n constructor(url) {\r\n this._url = url\r\n this._callbacks = {};\r\n this._reconnectTimer = null;\r\n this.init();\r\n }\r\n\r\n init() {\r\n this._ws = new WebSocket(this._url);\r\n this._ws.onopen = () => {\r\n this.onopen();\r\n };\r\n this._ws.onmessage = (e) => {\r\n this.onmessage(e);\r\n };\r\n this._ws.onclose = (e) => {\r\n if (e.code != 1000) { // 状态码:1000 正常关闭,不重新建立连接\r\n this.onclose(e);\r\n // 尝试重连\r\n this.reconnect();\r\n }\r\n }\r\n }\r\n\r\n onopen() {\r\n console.log('WebSocket open: ', this._url);\r\n }\r\n\r\n onmessage(e) {\r\n console.log('WebSocket message:', e.data);\r\n if (e.data) {\r\n const data = JSON.parse(e.data);\r\n const type = 'message';\r\n if (type in this._callbacks) {\r\n this._callbacks[type](data);\r\n }\r\n }\r\n }\r\n\r\n onclose(e) {\r\n console.log('WebSocket close:', e);\r\n }\r\n\r\n reconnect() {\r\n clearTimeout(this._reconnectTimer);\r\n this._reconnectTimer = setTimeout(() => {\r\n console.log('WebSocket reconnect:', this._url);\r\n this.init();\r\n }, 5000);\r\n }\r\n\r\n send(data) {\r\n if (this._ws.readyState === WebSocket.OPEN) {\r\n console.log('WebSocket send:', JSON.stringify(data));\r\n this._ws.send(JSON.stringify(data));\r\n }\r\n }\r\n\r\n registerCallback(type, callback) {\r\n this._callbacks[type] = callback;\r\n }\r\n}\r\n\r\nexport default WebSocketClient\r\n"],"mappings":"AAAA,MAAMA,eAAe,CAAC;EAClBC,WAAWA,CAACC,GAAG,EAAE;IACb,IAAI,CAACC,IAAI,GAAGD,GAAG;IACf,IAAI,CAACE,UAAU,GAAG,CAAC,CAAC;IACpB,IAAI,CAACC,eAAe,GAAG,IAAI;IAC3B,IAAI,CAACC,IAAI,CAAC,CAAC;EACf;EAEAA,IAAIA,CAAA,EAAG;IACH,IAAI,CAACC,GAAG,GAAG,IAAIC,SAAS,CAAC,IAAI,CAACL,IAAI,CAAC;IACnC,IAAI,CAACI,GAAG,CAACE,MAAM,GAAG,MAAM;MACpB,IAAI,CAACA,MAAM,CAAC,CAAC;IACjB,CAAC;IACD,IAAI,CAACF,GAAG,CAACG,SAAS,GAAI
|