]> git.r.bdr.sh - rbdr/junction/blobdiff - server/index.js
Use room all around
[rbdr/junction] / server / index.js
index de05616dd12d3253200295d0d10c3948882731a8..6493ba48992ead254f06706c228dd7ad5aeb2527 100644 (file)
@@ -1,54 +1,66 @@
-const PORT = 8000;
+import { Server } from 'socket.io';
+import { port } from './config.js';
+import * as events from './events.js';
 
 
-const express = require('express');
-const http = require('http');
-const main = express();
-const server = http.createServer(main);
-const io  = require('socket.io').listen(server);
-const events = require('./events');
+const server = new Server(port);
+console.log(`Listening on port ${port}`);
 
 
-server.listen(PORT, null, function() {
-    console.log("Listening on port " + PORT);
-});
-
-const sockets = {};
+const rooms = {};
 
 
-io.sockets.on('connection', (socket) => {
-    sockets[socket.id] = socket;
+server.on('connection', (socket) => {
     const me = socket.id;
 
     console.log(`[CONNECT] New client connected with ID ${me}`);
 
     const me = socket.id;
 
     console.log(`[CONNECT] New client connected with ID ${me}`);
 
-    socket.on('join', (data) => {
-        Object.entries(io.in(data.url).sockets).forEach(([peerId, peer]) => {
-            if (peerId !== me) {
-                peer.emit(events.types.ADD_PEER, events.addPeer(me, false));
-                socket.emit(events.types.ADD_PEER, events.addPeer(peerId, true));
-            }
-        });
+    socket.on('join', async (data) => {
+      const room = data.room;
+      socket.join(room);
+
+      if (!rooms[room]) {
+        rooms[room] = [];
+      }
+
+      rooms[room].push(socket.id);
 
 
-        socket.join(data.url);
-        console.log(`[CONNECT] Client ${me} added to room ${data.url}`);
+      const sockets = await server.in(room).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 ${room}`);
     });
 
     socket.on('disconnecting', () => {
     });
 
     socket.on('disconnecting', () => {
-        const rooms = Object.keys(socket.rooms);
-        rooms.forEach(room => io.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me)));
 
 
-        console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`);
+      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];
+          }
+        }
+      }
+      console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`);
     });
 
     });
 
-    socket.on('relayICECandidate', (data) => {
-        Object.keys(io.in(data.url).sockets).forEach(peer =>
-            peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate)));
+    socket.on('relayICECandidate', async (data) => {
 
 
+        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`);
     });
 
         console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`);
     });
 
-    socket.on('relaySessionDescription', (data) => {
-        Object.keys(io.in(data.url).sockets).forEach(peer =>
-            peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description)));
+    socket.on('relayOffer', async (data) => {
 
 
-        console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`);
+        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`);
     });
     });
-});
\ No newline at end of file
+
+    socket.on('relayAnswer', async (data) => {
+
+        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`);
+    });
+});