| 1 |
// https://socket.io/docs/v4/engine-io-protocol/#protocol |
| 2 |
const PACKET_TYPES = { |
| 3 |
OPEN: "0", |
| 4 |
CLOSE: "1", |
| 5 |
PING: "2", |
| 6 |
PONG: "3", |
| 7 |
MESSAGE: "4", |
| 8 |
}; |
| 9 |
|
| 10 |
// https://socket.io/docs/v4/socket-io-protocol/#exchange-protocol |
| 11 |
const MSG_TYPES = { |
| 12 |
CONNECT: "0", |
| 13 |
DISCONNECT: "1", |
| 14 |
EVENT: "2", |
| 15 |
ACK: "3", |
| 16 |
CONNECT_ERROR: "4", |
| 17 |
BINARY_EVENT: "5", |
| 18 |
BINARY_ACK: "6", |
| 19 |
}; |
| 20 |
|
| 21 |
const CONNECTION_TIMEOUT_MS = 10000; |
| 22 |
const RECONNECT_DELAY_MS = 5000; |
| 23 |
|
| 24 |
export class SocketIoService { |
| 25 |
/** |
| 26 |
* @param {import("models").SocketIoCallbacks} callbacks |
| 27 |
*/ |
| 28 |
constructor(callbacks) { |
| 29 |
this.setup(...arguments); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @param {import("models").SocketIoCallbacks} callbacks |
| 34 |
*/ |
| 35 |
setup(callbacks) { |
| 36 |
this.callbacks = callbacks; |
| 37 |
this._reset(); |
| 38 |
|
| 39 |
// Chrome slows down our pings when the tab is inactive, causing the websocket |
| 40 |
// to disconnect. Instead of this, we disconnect it ourselves when the tab |
| 41 |
// loses focus, and reconnect once focus is restored. |
| 42 |
document.addEventListener("visibilitychange", () => { |
| 43 |
if (!this.websocket) { |
| 44 |
return; |
| 45 |
} |
| 46 |
if (document.visibilityState === "hidden") { |
| 47 |
this.websocket.close(); |
| 48 |
} else if (document.visibilityState === "visible" && this.closed) { |
| 49 |
this.connect(this.currentUrl); |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
_reset() { |
| 55 |
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) { |
| 56 |
this.websocket.onclose = null; |
| 57 |
this.websocket.close(); |
| 58 |
} |
| 59 |
this.websocket = null; |
| 60 |
this.currentUrl = null; |
| 61 |
this.socketId = null; |
| 62 |
|
| 63 |
if (this.pingIntervalId) { |
| 64 |
clearInterval(this.pingIntervalId); |
| 65 |
} |
| 66 |
this.pingIntervalId = null; |
| 67 |
if (this.reconnectTimeoutId) { |
| 68 |
clearInterval(this.reconnectTimeoutId); |
| 69 |
} |
| 70 |
this.reconnectTimeoutId = null; |
| 71 |
|
| 72 |
if (this.pongTimeoutIds) { |
| 73 |
for (const timeoutId of this.pongTimeoutIds) { |
| 74 |
clearTimeout(timeoutId); |
| 75 |
} |
| 76 |
} |
| 77 |
this.pongTimeoutIds = []; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param {string} url |
| 82 |
*/ |
| 83 |
connect(url) { |
| 84 |
if (this.websocket) { |
| 85 |
this._reset(); |
| 86 |
} |
| 87 |
this.currentUrl = url; |
| 88 |
this.websocket = new WebSocket(this.currentUrl); |
| 89 |
|
| 90 |
this.websocket.onclose = () => { |
| 91 |
if (this.pingIntervalId) { |
| 92 |
clearInterval(this.pingIntervalId); |
| 93 |
} |
| 94 |
if (document.visibilityState === "visible") { |
| 95 |
this.callbacks.onClose(); |
| 96 |
this.reconnectTimeoutId = setTimeout( |
| 97 |
() => this.connect(this.currentUrl), |
| 98 |
RECONNECT_DELAY_MS |
| 99 |
); |
| 100 |
} |
| 101 |
}; |
| 102 |
this.websocket.onmessage = (event) => this._onMessageReceived(event.data); |
| 103 |
} |
| 104 |
|
| 105 |
sendMessage(message) { |
| 106 |
const messageArray = Array.isArray(message) ? message : [message]; |
| 107 |
const jsonData = JSON.stringify(messageArray); |
| 108 |
const payload = `${MSG_TYPES.EVENT}${jsonData}`; |
| 109 |
this._sendPacket(PACKET_TYPES.MESSAGE, payload); |
| 110 |
} |
| 111 |
|
| 112 |
get closed() { |
| 113 |
return this.websocket?.readyState !== WebSocket.OPEN; |
| 114 |
} |
| 115 |
|
| 116 |
_sendPacket(packetType, data) { |
| 117 |
this.websocket.send(`${packetType}${data}`); |
| 118 |
} |
| 119 |
|
| 120 |
_handleOpenMessage(data) { |
| 121 |
const info = JSON.parse(data); |
| 122 |
this.socketId = info.sid; |
| 123 |
this.pingIntervalId = setInterval(() => { |
| 124 |
this.websocket.send(PACKET_TYPES.PING); |
| 125 |
const pongTimeoutId = setTimeout(() => { |
| 126 |
this.websocket.close(); |
| 127 |
}, CONNECTION_TIMEOUT_MS); |
| 128 |
this.pongTimeoutIds.push(pongTimeoutId); |
| 129 |
}, info.pingInterval); |
| 130 |
} |
| 131 |
|
| 132 |
async _handleMessage(data) { |
| 133 |
const messageType = data[0]; |
| 134 |
const messageBody = data.slice(1); |
| 135 |
switch (messageType) { |
| 136 |
case MSG_TYPES.CONNECT: { |
| 137 |
this.callbacks.onConnect(); |
| 138 |
return; |
| 139 |
} |
| 140 |
case MSG_TYPES.EVENT: { |
| 141 |
const body = JSON.parse(messageBody); |
| 142 |
if (!Array.isArray(body) || body.length === 0) { |
| 143 |
this.websocket.close(); |
| 144 |
return; |
| 145 |
} |
| 146 |
this.callbacks.onEvent(body); |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
_onMessageReceived(data) { |
| 153 |
if (data instanceof Blob) { |
| 154 |
this.callbacks.onBinaryEvent(data); |
| 155 |
return; |
| 156 |
} |
| 157 |
const packetType = data[0]; |
| 158 |
const packetData = data.slice(1); |
| 159 |
if (packetType === PACKET_TYPES.OPEN) { |
| 160 |
this._handleOpenMessage(packetData); |
| 161 |
} else if (packetType === PACKET_TYPES.PONG) { |
| 162 |
for (const timeoutId of this.pongTimeoutIds) { |
| 163 |
clearTimeout(timeoutId); |
| 164 |
} |
| 165 |
this.pongTimeoutIds = []; |
| 166 |
} else if (packetType === PACKET_TYPES.MESSAGE) { |
| 167 |
this._handleMessage(packetData); |
| 168 |
} |
| 169 |
} |
| 170 |
} |