X-Git-Url: https://git.r.bdr.sh/rbdr/junction/blobdiff_plain/8c7fb565690890d57b6af20ad0771a48cf82b4c7..5685e249653cae8c2ba97c0c59c45e3221d8ffa2:/server/index.js diff --git a/server/index.js b/server/index.js index b5d5d1b..e60795f 100644 --- a/server/index.js +++ b/server/index.js @@ -1,55 +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); -console.log(`Listening on port ${port}`); - -const sockets = {}; - -server.on('connection', (socket) => { - sockets[socket.id] = socket; - const me = socket.id; - - console.log(`[CONNECT] New client connected with ID ${me}`); - - socket.on('join', async (data) => { - socket.join(data.url); - const sockets = await server.in(data.url).fetchSockets() - console.log(sockets); - 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}`); - }); +console.debug(`Listening on port ${port}`); - socket.on('disconnecting', () => { - const rooms = Object.keys(socket.rooms); - rooms.forEach(room => server.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me))); +const rooms = {}; - console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`); - }); +server.on(events.types.CONNECTION, (socket) => { + const me = socket.id; - socket.on('relayICECandidate', async (data) => { - const sockets = await server.in(data.url).fetchSockets() - console.log(sockets); - sockets.forEach((peer) => { - peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate)) - }); + console.debug(`[CONNECT] New client connected with ID ${me}`); - console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`); - }); + socket.on(events.types.JOIN, async ({ room }) => { + socket.join(room); + + if (!rooms[room]) { + rooms[room] = []; + } - socket.on('relaySessionDescription', async (data) => { - const sockets = await server.in(data.url).fetchSockets() - console.log(sockets); - sockets.forEach((peer) => { - peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description)) - }); + rooms[room].push(socket.id); - console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`); + 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(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]; + } + } + } + }); + + socket.on(events.types.RELAY_ICE_CANDIDATE, async ({ candidate, peerId }) => { + events.ICECandidateReceived(me, candidate)(socket.to(peerId)); + }); + + socket.on(events.types.RELAY_OFFER, async ({ offer, peerId }) => { + events.offerReceived(me, offer)(socket.to(peerId)); + }); + + socket.on(events.types.RELAY_ANSWER, async ({ answer, peerId }) => { + events.answerReceived(me, answer)(socket.to(peerId)); + }); });