]> git.r.bdr.sh - rbdr/junction/blobdiff - server/index.js
Add command for the server
[rbdr/junction] / server / index.js
index ebdede7df2d4960ae39377f6026e99006e186218..56a1df48e84b4411ea51b4ea49c9511bee93286a 100644 (file)
@@ -1,52 +1,55 @@
-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');
-
-server.listen(PORT, null, function() {
-    console.log("Listening on port " + PORT);
-});
+const server = new Server(port);
+console.log(`Listening on port ${port}`);
 
 const sockets = {};
 
 
 const sockets = {};
 
-io.sockets.on('connection', (socket) => {
+server.on('connection', (socket) => {
     sockets[socket.id] = socket;
     const me = socket.id;
 
     console.log(`[CONNECT] New client connected with ID ${me}`);
 
     socket.on('join', (data) => {
     sockets[socket.id] = socket;
     const me = socket.id;
 
     console.log(`[CONNECT] New client connected with ID ${me}`);
 
     socket.on('join', (data) => {
-        Object.keys(io.in(data.url).sockets).forEach(peer => {
-            peer.emit(events.types.ADD_PEER, events.addPeer(me, false));
-            socket.emit(events.types.ADD_PEER, events.addPeer(peer, true));
-        });
-
         socket.join(data.url);
         socket.join(data.url);
+        const sockets = server.sockets.adapter.rooms.get(data.url);
+        sockets.forEach((peerId) => {
+            if (peerId !== me) {
+                const peer = server.sockets.sockets.get(peerId)
+                peer.emit(events.types.ADD_PEER, events.addPeer(me, false));
+                socket.emit(events.types.ADD_PEER, events.addPeer(peerId, true));
+            }
+        });
         console.log(`[CONNECT] Client ${me} added to room ${data.url}`);
     });
 
     socket.on('disconnecting', () => {
         const rooms = Object.keys(socket.rooms);
         console.log(`[CONNECT] Client ${me} added to room ${data.url}`);
     });
 
     socket.on('disconnecting', () => {
         const rooms = Object.keys(socket.rooms);
-        rooms.forEach(room => io.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me)));
+        rooms.forEach(room => server.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me)));
 
         console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`);
     });
 
     socket.on('relayICECandidate', (data) => {
 
         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)));
+        const sockets = server.sockets.adapter.rooms.get(data.url);
+        sockets.forEach((peerId) => {
+            const peer = server.sockets.sockets.get(peerId)
+            peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate))
+        });
 
         console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`);
     });
 
     socket.on('relaySessionDescription', (data) => {
 
         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)));
+        const sockets = server.sockets.adapter.rooms.get(data.url);
+        sockets.forEach((peerId) => {
+            const peer = server.sockets.sockets.get(peerId)
+            peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description))
+        });
 
         console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`);
     });
 
         console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`);
     });
-});
\ No newline at end of file
+});