| 20 | |
| 21 | export default defineWebSocketHandler({ |
| 22 | open(peer) { |
| 23 | const identity = createIdentity() |
| 24 | peer.context.identity = identity |
| 25 | |
| 26 | peer.subscribe(CHANNEL) |
| 27 | |
| 28 | // Capture the roster of existing peers *before* adding self, so `welcome.peers` |
| 29 | // excludes the new peer (the client tracks self separately via `welcome.self`). |
| 30 | const roster = [...peers.values()] |
| 31 | peers.set(identity.id, identity) |
| 32 | |
| 33 | send(peer, { t: 'welcome', self: identity, peers: roster }) |
| 34 | peer.publish(CHANNEL, JSON.stringify({ t: 'join', peer: identity } satisfies ServerMessage)) |
| 35 | }, |
| 36 | message(peer, message) { |
| 37 | const identity = peer.context.identity as Peer | undefined |
| 38 | if (!identity) return |