Skip to content

Commit

Permalink
fix: in ws v8, messages are buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudd2 authored Jul 1, 2023
1 parent a272d78 commit c22d11a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 60 deletions.
38 changes: 18 additions & 20 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,24 @@ export function startServer(port: number, device: string | null = null, enableCo
wss.on("connection", (ws) => {
clients.push(ws);
ws.on("message", (message) => {
if (typeof message === "string") {
const msg = JSON.parse(message);
switch (msg.c) {
case "ping":
ws.send(JSON.stringify({c: "pong"}));
break;
case "limp":
if (ebb) { ebb.disableMotors(); }
break;
case "setPenHeight":
if (ebb) {
(async () => {
if (await ebb.supportsSR()) {
await ebb.setServoPowerTimeout(10000, true)
}
await ebb.setPenHeight(msg.p.height, msg.p.rate);
})();
}
break;
}
const msg = JSON.parse(message.toString());
switch (msg.c) {
case "ping":
ws.send(JSON.stringify({c: "pong"}));
break;
case "limp":
if (ebb) { ebb.disableMotors(); }
break;
case "setPenHeight":
if (ebb) {
(async () => {
if (await ebb.supportsSR()) {
await ebb.setServoPowerTimeout(10000, true)
}
await ebb.setPenHeight(msg.p.height, msg.p.rate);
})();
}
break;
}
});

Expand Down
78 changes: 38 additions & 40 deletions src/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,46 +250,44 @@ class SaxiDriver implements Driver {
this.pingInterval = window.setInterval(() => this.ping(), 30000);
});
this.socket.addEventListener("message", (e: MessageEvent) => {
if (typeof e.data === "string") {
const msg = JSON.parse(e.data);
switch (msg.c) {
case "pong": {
// nothing
} break;
case "progress": {
if (this.onprogress != null) {
this.onprogress(msg.p.motionIdx);
}
} break;
case "cancelled": {
if (this.oncancelled != null) {
this.oncancelled();
}
} break;
case "finished": {
if (this.onfinished != null) {
this.onfinished();
}
} break;
case "dev": {
if (this.ondevinfo != null) {
this.ondevinfo(msg.p);
}
} break;
case "pause": {
if (this.onpause != null) {
this.onpause(msg.p.paused)
}
} break;
case "plan": {
if (this.onplan != null) {
this.onplan(Plan.deserialize(msg.p.plan))
}
} break;
default: {
console.log("Unknown message from server:", msg);
} break;
}
const msg = JSON.parse(e.data);
switch (msg.c) {
case "pong": {
// nothing
} break;
case "progress": {
if (this.onprogress != null) {
this.onprogress(msg.p.motionIdx);
}
} break;
case "cancelled": {
if (this.oncancelled != null) {
this.oncancelled();
}
} break;
case "finished": {
if (this.onfinished != null) {
this.onfinished();
}
} break;
case "dev": {
if (this.ondevinfo != null) {
this.ondevinfo(msg.p);
}
} break;
case "pause": {
if (this.onpause != null) {
this.onpause(msg.p.paused)
}
} break;
case "plan": {
if (this.onplan != null) {
this.onplan(Plan.deserialize(msg.p.plan))
}
} break;
default: {
console.log("Unknown message from server:", msg);
} break;
}
});
this.socket.addEventListener("error", () => {
Expand Down

0 comments on commit c22d11a

Please sign in to comment.