-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_server.ts
58 lines (46 loc) · 1.7 KB
/
signal_server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// signal_server_broadcast.ts
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
// Global map to store BroadcastChannels per room
const broadcastChannels: Map<string, BroadcastChannel> = new Map()
serve((req) => {
const { searchParams } = new URL(req.url)
const room = searchParams.get('room') || 'default'
if (req.headers.get('upgrade') !== 'websocket') {
return new Response('Yjs WebRTC Signaling Server with BroadcastChannel', {
status: 200,
})
}
const { socket, response } = Deno.upgradeWebSocket(req)
// Retrieve or create a BroadcastChannel for the room
let broadcast = broadcastChannels.get(room)
if (!broadcast) {
broadcast = new BroadcastChannel(`room_${room}`)
broadcastChannels.set(room, broadcast)
}
// Handler for incoming WebSocket messages
socket.onmessage = (event) => {
// Broadcast the message to all other clients in the room
broadcast.postMessage(event.data)
}
// Handler for messages from the BroadcastChannel
const onBroadcastMessage = (event: MessageEvent) => {
if (socket.readyState === WebSocket.OPEN) {
socket.send(event.data)
}
}
// Listen to the BroadcastChannel
broadcast.addEventListener('message', onBroadcastMessage)
// Cleanup when the WebSocket is closed
socket.onclose = () => {
broadcast?.removeEventListener('message', onBroadcastMessage)
// Optionally remove the BroadcastChannel if no clients are connected
// This requires tracking the number of clients per room
}
// Handle WebSocket errors
socket.onerror = (err) => {
console.error('WebSocket error:', err)
socket.close()
broadcast?.removeEventListener('message', onBroadcastMessage)
}
return response
})