MCPcopy Create free account
hub / github.com/vercel/examples / message

Function message

websockets/nitro/server/api/ws.ts:36–68  ·  view source on GitHub ↗
(peer, message)

Source from the content-addressed store, hash-verified

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
39
40 let msg: ClientMessage
41 try {
42 msg = JSON.parse(message.text()) as ClientMessage
43 }
44 catch {
45 return
46 }
47
48 switch (msg.t) {
49 case 'cursor': {
50 const x = toUnit(msg.x)
51 const y = toUnit(msg.y)
52 if (x === null || y === null) return
53 peer.publish(CHANNEL, JSON.stringify({ t: 'cursor', id: identity.id, x, y } satisfies ServerMessage))
54 break
55 }
56 case 'reaction': {
57 const x = toUnit(msg.x)
58 const y = toUnit(msg.y)
59 if (x === null || y === null) return
60 if (!(REACTIONS as readonly string[]).includes(msg.emoji)) return
61 peer.publish(CHANNEL, JSON.stringify({ t: 'reaction', id: identity.id, emoji: msg.emoji, x, y } satisfies ServerMessage))
62 break
63 }
64 case 'ping':
65 send(peer, { t: 'pong' })
66 break
67 }
68 },
69 close(peer) {
70 const identity = peer.context.identity as Peer | undefined
71 if (!identity) return

Callers

nothing calls this directly

Calls 3

publishMethod · 0.80
toUnitFunction · 0.70
sendFunction · 0.70

Tested by

no test coverage detected