| 3 | |
| 4 | export default { |
| 5 | async onConnect(ws, room) { |
| 6 | // your business logic here |
| 7 | ws.send(`count:${(await room.storage.get("count")) || "0"}`); |
| 8 | |
| 9 | ws.addEventListener("message", async function incoming(evt) { |
| 10 | if (evt.data === "increment") { |
| 11 | await room.storage.put( |
| 12 | "count", |
| 13 | (parseInt(`${await room.storage.get("count")}`) || 0) + 1 |
| 14 | ); |
| 15 | room.broadcast(`count:${(await room.storage.get("count")) || "0"}`); |
| 16 | } else if (evt.data === "decrement") { |
| 17 | await room.storage.put( |
| 18 | "count", |
| 19 | (parseInt(`${await room.storage.get("count")}`) || 0) - 1 |
| 20 | ); |
| 21 | room.broadcast(`count:${(await room.storage.get("count")) || "0"}`); |
| 22 | } else if ((evt.data as string).startsWith("latency")) { |
| 23 | ws.send(evt.data); |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | } satisfies PartyKitServer; |