]> git.r.bdr.sh - rbdr/junction/blame_incremental - server/index.js
Fix typo
[rbdr/junction] / server / index.js
... / ...
CommitLineData
1import { Server } from "socket.io";
2import { port } from "./config.js";
3import * as events from "./events.js";
4
5const server = new Server(port);
6console.debug(`Listening on port ${port}`);
7
8const rooms = {};
9
10server.on(events.types.CONNECTION, (socket) => {
11 const me = socket.id;
12
13 console.debug(`[CONNECT] New client connected with ID ${me}`);
14
15 socket.on(events.types.JOIN, async ({ room }) => {
16 socket.join(room);
17
18 if (!rooms[room]) {
19 rooms[room] = [];
20 }
21
22 rooms[room].push(socket.id);
23
24 const sockets = await server.in(room).fetchSockets();
25 sockets.forEach((peer) => {
26 if (peer.id !== me) {
27 events.addPeer(me, false)(peer);
28 events.addPeer(peer.id, true)(socket);
29 }
30 });
31 });
32
33 socket.on(events.types.DISCONNECTING, () => {
34 for (const room in rooms) {
35 if (rooms[room].includes(me)) {
36 rooms[room] = rooms[room].filter((id) => id !== me);
37 events.removePeer(me)(socket.to(room));
38 if (rooms[room].length === 0) {
39 delete rooms[room];
40 }
41 }
42 }
43 });
44
45 socket.on(events.types.RELAY_ICE_CANDIDATE, async ({ candidate, peerId }) => {
46 events.ICECandidateReceived(me, candidate)(socket.to(peerId));
47 });
48
49 socket.on(events.types.RELAY_OFFER, async ({ offer, peerId }) => {
50 events.offerReceived(me, offer)(socket.to(peerId));
51 });
52
53 socket.on(events.types.RELAY_ANSWER, async ({ answer, peerId }) => {
54 events.answerReceived(me, answer)(socket.to(peerId));
55 });
56});