]> git.r.bdr.sh - rbdr/junction/blobdiff - server/index.js
Inject script only once
[rbdr/junction] / server / index.js
index e6a37c7fe71c78ccfa781e94836485b1001e5f37..e60795f8543e470edef67051d90d25e3484876f6 100644 (file)
@@ -1,66 +1,56 @@
-import { Server } from 'socket.io';
-import { port } from './config.js';
-import * as events from './events.js';
+import { Server } from "socket.io";
+import { port } from "./config.js";
+import * as events from "./events.js";
 
 const server = new Server(port);
 
 const server = new Server(port);
-console.log(`Listening on port ${port}`);
+console.debug(`Listening on port ${port}`);
 
 const rooms = {};
 
 
 const rooms = {};
 
-server.on('connection', (socket) => {
-    const me = socket.id;
+server.on(events.types.CONNECTION, (socket) => {
+  const me = socket.id;
 
 
-    console.log(`[CONNECT] New client connected with ID ${me}`);
+  console.debug(`[CONNECT] New client connected with ID ${me}`);
 
 
-    socket.on('join', async (data) => {
-      const room = data.room;
-      socket.join(room);
+  socket.on(events.types.JOIN, async ({ room }) => {
+    socket.join(room);
 
 
-      if (!rooms[room]) {
-        rooms[room] = {};
-      }
+    if (!rooms[room]) {
+      rooms[room] = [];
+    }
 
 
-      rooms[room].push(socket.id);
+    rooms[room].push(socket.id);
 
 
-      const sockets = await server.in(data.url).fetchSockets()
-      sockets.forEach((peer) => {
-        if (peer.id !== me) {
-          peer.emit(events.types.ADD_PEER, events.addPeer(me, false));
-          socket.emit(events.types.ADD_PEER, events.addPeer(peer.id, true));
-        }
-      });
-      console.log(`[CONNECT] Client ${me} added to room ${data.url}`);
+    const sockets = await server.in(room).fetchSockets();
+    sockets.forEach((peer) => {
+      if (peer.id !== me) {
+        events.addPeer(me, false)(peer);
+        events.addPeer(peer.id, true)(socket);
+      }
     });
     });
-
-    socket.on('disconnecting', () => {
-
-      for (const room in rooms) {
-        if (rooms[room].includes(me)) {
-          rooms[room] = rooms[room].filter(id => id !== me);
-          socket.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me));
-          if (rooms[room].length === 0) {
-            delete rooms[room];
-          }
+  });
+
+  socket.on(events.types.DISCONNECTING, () => {
+    for (const room in rooms) {
+      if (rooms[room].includes(me)) {
+        rooms[room] = rooms[room].filter((id) => id !== me);
+        events.removePeer(me)(socket.to(room));
+        if (rooms[room].length === 0) {
+          delete rooms[room];
         }
       }
         }
       }
-      console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`);
-    });
+    }
+  });
 
 
-    socket.on('relayICECandidate', async (data) => {
+  socket.on(events.types.RELAY_ICE_CANDIDATE, async ({ candidate, peerId }) => {
+    events.ICECandidateReceived(me, candidate)(socket.to(peerId));
+  });
 
 
-        socket.to(data.peerId).emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.candidate))
-        console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`);
-    });
-
-    socket.on('relayOffer', async (data) => {
-
-        socket.to(data.peerId).emit(events.types.OFFER_RECEIVED, events.offerReceived(me, data.offer))
-        console.log(`[RELAY_OFFER] ICE offer for client ${me} has been relayed to all peers`);
-    });
-
-    socket.on('relayAnswer', async (data) => {
+  socket.on(events.types.RELAY_OFFER, async ({ offer, peerId }) => {
+    events.offerReceived(me, offer)(socket.to(peerId));
+  });
 
 
-        socket.to(data.peerId).emit(events.types.ANSWER_RECEIVED, events.answerReceived(me, data.answer))
-        console.log(`[RELAY_OFFER] ICE offer for client ${me} has been relayed to all peers`);
-    });
+  socket.on(events.types.RELAY_ANSWER, async ({ answer, peerId }) => {
+    events.answerReceived(me, answer)(socket.to(peerId));
+  });
 });
 });