chore: send message when websocket is open
This commit is contained in:
1 parent
651ab3e7e3
commit
19d0a9af28
1 file changed
+8
-10
@@ -1,10 +1,7 @@
|
|||||||
function main() {
|
function main() {
|
||||||
let root = window.document.getElementById("main");
|
let root = window.document.getElementById("main");
|
||||||
|
|
||||||
if (root != null) {
|
if (root != null) {
|
||||||
// create a new ipc
|
|
||||||
window.ipc = new IPC(root);
|
window.ipc = new IPC(root);
|
||||||
window.ipc.postMessage(serializeIpcMessage("initialize"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,21 +10,22 @@ class IPC {
|
|||||||
// connect to the websocket
|
// connect to the websocket
|
||||||
window.interpreter = new Interpreter(root);
|
window.interpreter = new Interpreter(root);
|
||||||
|
|
||||||
this.ws = new WebSocket(WS_ADDR);
|
let ws = new WebSocket(WS_ADDR);
|
||||||
|
|
||||||
this.ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
console.log("Connected to the websocket");
|
window.ipc.postMessage(serializeIpcMessage("initialize"));
|
||||||
};
|
};
|
||||||
|
|
||||||
this.ws.onerror = (err) => {
|
ws.onerror = (err) => {
|
||||||
console.error("Error: ", err);
|
// todo: retry the connection
|
||||||
};
|
};
|
||||||
|
|
||||||
this.ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
console.log("Received message: ", event.data);
|
|
||||||
let edits = JSON.parse(event.data);
|
let edits = JSON.parse(event.data);
|
||||||
window.interpreter.handleEdits(edits);
|
window.interpreter.handleEdits(edits);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.ws = ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
postMessage(msg) {
|
postMessage(msg) {
|
||||||
|
|||||||
Reference in new issue
Block a user