(event: EVENT)
| 194 | // Event Handlers |
| 195 | |
| 196 | protected async onMessage(event: EVENT): Promise<void> { |
| 197 | |
| 198 | // Parse |
| 199 | |
| 200 | const [parsed_data, parse_error] = parse_message_data(event.data); |
| 201 | |
| 202 | if (parse_error) { |
| 203 | console.error('Failed to parse message data.', parse_error || ''); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | // Validate |
| 208 | |
| 209 | const [data, data_error] = validate_socket_message<any>(parsed_data); |
| 210 | |
| 211 | if (!data || data_error) { |
| 212 | console.error('Failed to validate message data.', data_error || ''); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | // Handle |
| 217 | |
| 218 | const [inc, out] = await api_handle_message(this.api, data, event); |
| 219 | |
| 220 | if (inc) { |
| 221 | const index = this.client.sent.findIndex(sent => sent.id === inc.id); |
| 222 | const sent = this.client.sent[index]; |
| 223 | if (sent) { |
| 224 | |
| 225 | this.client.sent.splice(index, 1); |
| 226 | sent.resolve(inc as SocketResponseData<BackInTemplate[BackIn]>); |
| 227 | } else { |
| 228 | console.error('Socket Client - Received a response with and ID of a request that is not being handled.'); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (out && this.client.socket) { |
| 233 | this.client.socket.send(JSON.stringify(out)); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | protected onError(err: any): void { |
| 238 | console.log(`Socket Error - ${err}`); |
nothing calls this directly
no test coverage detected