X-Git-Url: https://git.r.bdr.sh/rbdr/junction/blobdiff_plain/ed9f8fd50086439ff3de12092af2f813763ada91..6a3568bc5942423b93ba2749036489ad9e38e7cf:/server/index.js diff --git a/server/index.js b/server/index.js index 0cfef25..8ac427b 100644 --- a/server/index.js +++ b/server/index.js @@ -1,49 +1,56 @@ -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.debug(`Listening on port ${port}`); -server.listen(PORT, null, function() { - console.log("Listening on port " + PORT); -}); - -const sockets = {}; - -io.sockets.on('connection', (socket) => { - sockets[socket.id] = socket; - const me = socket.id; - - console.log(`[CONNECT] New client connected with ID ${me}`); - - socket.on('join', (data) => { - socket.join(data.url); - Object.keys(io.in(data.url).sockets).forEach(peer => peer.emit(events.types.ADD_PEER, events.addPeer(me, peer.id === me))); +const rooms = {}; - console.log(`[CONNECT] Client ${me} added to room ${data.url}`); - }); +server.on(events.types.CONNECTION, (socket) => { + const me = socket.id; - socket.on('disconnecting', () => { - const rooms = Object.keys(socket.rooms); - rooms.forEach(room => io.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me))); + console.debug(`[CONNECT] New client connected with ID ${me}`); - console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`); - }); + socket.on(events.types.JOIN, async ({ room }) => { + socket.join(room); - 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))); + if (!rooms[room]) { + rooms[room] = []; + } - console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`); - }); + rooms[room].push(socket.id); - 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))); - - 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)(peer); + events.addPeer(peer.id)(socket); + } }); -}); \ No newline at end of file + }); + + 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)); + }); +});