|
|
|
@ -2,6 +2,7 @@ let websocket: WebSocket; |
|
|
|
const msgHandlersMap = {}; |
|
|
|
const msgHandlersMap = {}; |
|
|
|
let lastUrl; |
|
|
|
let lastUrl; |
|
|
|
let closeFlag = false; |
|
|
|
let closeFlag = false; |
|
|
|
|
|
|
|
const messageQueue: any[] = []; |
|
|
|
|
|
|
|
|
|
|
|
export const closeWebSocket = () => { |
|
|
|
export const closeWebSocket = () => { |
|
|
|
closeFlag = false; |
|
|
|
closeFlag = false; |
|
|
|
@ -9,7 +10,11 @@ export const closeWebSocket = () => { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export const sendSocketMsg = (msg) => { |
|
|
|
export const sendSocketMsg = (msg) => { |
|
|
|
websocket?.send(JSON.stringify(msg)); |
|
|
|
if (websocket && websocket.readyState === WebSocket.OPEN) { |
|
|
|
|
|
|
|
websocket.send(JSON.stringify(msg)); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
messageQueue.push(msg); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export const openWebSocket = (url) => { |
|
|
|
export const openWebSocket = (url) => { |
|
|
|
@ -24,6 +29,11 @@ export const openWebSocket = (url) => { |
|
|
|
websocket.onopen = () => { |
|
|
|
websocket.onopen = () => { |
|
|
|
console.log("websocket已连接"); |
|
|
|
console.log("websocket已连接"); |
|
|
|
closeFlag = true; |
|
|
|
closeFlag = true; |
|
|
|
|
|
|
|
// Send any queued messages
|
|
|
|
|
|
|
|
while (messageQueue.length > 0) { |
|
|
|
|
|
|
|
const msg = messageQueue.shift(); |
|
|
|
|
|
|
|
websocket.send(JSON.stringify(msg)); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
websocket.onmessage = (msg) => { |
|
|
|
websocket.onmessage = (msg) => { |
|
|
|
const event = JSON.parse(msg.data); |
|
|
|
const event = JSON.parse(msg.data); |
|
|
|
|