Skip to content

Commit

Permalink
chore(example): handle binary/blob messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 4, 2024
1 parent 05ded7b commit 38c6baa
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions examples/h3/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@
log("ws", "Connecting to", url, "...");
ws = new WebSocket(url);

ws.addEventListener("message", (event) => {
const { user = "system", message = "" } = event.data.startsWith("{")
? JSON.parse(event.data)
: { message: event.data };
ws.addEventListener("message", async (event) => {
let data =
typeof event.data === "string" ? data : await event.data.text();
const { user = "system", message = "" } = data.startsWith("{")
? JSON.parse(data)
: { message: data };
log(
user,
typeof message === "string" ? message : JSON.stringify(message),
Expand Down

0 comments on commit 38c6baa

Please sign in to comment.