···11+import type * as Party from "partykit/server";
22+33+export default class Server implements Party.Server {
44+ constructor(readonly room: Party.Room) {}
55+66+ onConnect(conn: Party.Connection, ctx: Party.ConnectionContext) {
77+ // A websocket just connected!
88+ console.log(
99+ `Connected:
1010+ id: ${conn.id}
1111+ room: ${this.room.id}
1212+ url: ${new URL(ctx.request.url).pathname}`
1313+ );
1414+1515+ // let's send a message to the connection
1616+ conn.send("hello from server");
1717+ }
1818+1919+ onMessage(message: string, sender: Party.Connection) {
2020+ // let's log the message
2121+ console.log(`connection ${sender.id} sent message: ${message}`);
2222+ // as well as broadcast it to all the other connections in the room...
2323+ this.room.broadcast(
2424+ `${sender.id}: ${message}`,
2525+ // ...except for the connection it came from
2626+ [sender.id]
2727+ );
2828+ }
2929+}
3030+3131+Server satisfies Party.Worker;