aboutsummaryrefslogtreecommitdiff
path: root/ui/js/socket.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/js/socket.js')
-rw-r--r--ui/js/socket.js19
1 files changed, 19 insertions, 0 deletions
diff --git a/ui/js/socket.js b/ui/js/socket.js
new file mode 100644
index 0000000..46edb8d
--- /dev/null
+++ b/ui/js/socket.js
@@ -0,0 +1,19 @@
+import api from './api.js';
+
+var ws = new WebSocket('ws://localhost:8081/socket');
+export default ws;
+
+ws.onmessage = 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);
+};
+