chore: send message when websocket is open
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) {
