Przeglądaj źródła

chore: send message when websocket is open

Jonathan Kelley 2 lat temu
rodzic
commit
19d0a9af28
1 zmienionych plików z 8 dodań i 10 usunięć
  1. 8 10
      packages/liveview/src/main.js

+ 8 - 10
packages/liveview/src/main.js

@@ -1,10 +1,7 @@
 function main() {
   let root = window.document.getElementById("main");
-
   if (root != null) {
-    // create a new ipc
     window.ipc = new IPC(root);
-    window.ipc.postMessage(serializeIpcMessage("initialize"));
   }
 }
 
@@ -13,21 +10,22 @@ class IPC {
     // connect to the websocket
     window.interpreter = new Interpreter(root);
 
-    this.ws = new WebSocket(WS_ADDR);
+    let ws = new WebSocket(WS_ADDR);
 
-    this.ws.onopen = () => {
-      console.log("Connected to the websocket");
+    ws.onopen = () => {
+      window.ipc.postMessage(serializeIpcMessage("initialize"));
     };
 
-    this.ws.onerror = (err) => {
-      console.error("Error: ", err);
+    ws.onerror = (err) => {
+      // todo: retry the connection
     };
 
-    this.ws.onmessage = (event) => {
-      console.log("Received message: ", event.data);
+    ws.onmessage = (event) => {
       let edits = JSON.parse(event.data);
       window.interpreter.handleEdits(edits);
     };
+
+    this.ws = ws;
   }
 
   postMessage(msg) {