blob: bb739d50cc3a4106c69c2906ade0d115cf0d8df5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import api from './api.js';
var ws = new WebSocket('ws://localhost:8081/');
export default ws;
ws.addEventListener("message", ev => {
// this will already throw an error if the message doesn't contain valid JSON
const msg = JSON.parse(ev.data);
// check if api.msg.handle has a handler for msg.type
if (!api.msg.handle.hasOwnProperty(msg.type)) {
console.warn(`No message handler for type ${msg.type}`, msg);
return;
}
// run the appropriate message handler
api.msg.handle[msg.type](msg);
});
ws.addEventListener("close", () => {
console.error("WebSocket closed!");
});
|