MCPcopy
hub / github.com/partykit/partykit / onConnect

Function onConnect

examples/persistence/src/server.ts:5–26  ·  view source on GitHub ↗
(ws, room)

Source from the content-addressed store, hash-verified

3
4export 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;

Callers

nothing calls this directly

Calls 4

putMethod · 0.80
getMethod · 0.65
sendMethod · 0.45
broadcastMethod · 0.45

Tested by

no test coverage detected