]> git.r.bdr.sh - rbdr/junction/blame - server/index.js
Fix server
[rbdr/junction] / server / index.js
CommitLineData
80172072
RBR
1import { Server } from 'socket.io';
2import { port } from './config.js';
3import * as events from './events.js';
ed9f8fd5 4
80172072 5const server = new Server(port);
baea04f1 6console.log(`Listening on port ${port}`);
ed9f8fd5 7
8const sockets = {};
9
80172072 10server.on('connection', (socket) => {
ed9f8fd5 11 sockets[socket.id] = socket;
12 const me = socket.id;
13
14 console.log(`[CONNECT] New client connected with ID ${me}`);
15
8c7fb565 16 socket.on('join', async (data) => {
80172072 17 socket.join(data.url);
8c7fb565
RBR
18 const sockets = await server.in(data.url).fetchSockets()
19 console.log(sockets);
20 sockets.forEach((peer) => {
21 if (peer.id !== me) {
c3fafc92 22 peer.emit(events.types.ADD_PEER, events.addPeer(me, false));
8c7fb565 23 socket.emit(events.types.ADD_PEER, events.addPeer(peer.id, true));
c3fafc92 24 }
ad7fccbe 25 });
ed9f8fd5 26 console.log(`[CONNECT] Client ${me} added to room ${data.url}`);
27 });
28
29 socket.on('disconnecting', () => {
30 const rooms = Object.keys(socket.rooms);
80172072 31 rooms.forEach(room => server.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me)));
ed9f8fd5 32
33 console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`);
34 });
35
8c7fb565
RBR
36 socket.on('relayICECandidate', async (data) => {
37 const sockets = await server.in(data.url).fetchSockets()
38 console.log(sockets);
39 sockets.forEach((peer) => {
80172072
RBR
40 peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate))
41 });
ed9f8fd5 42
43 console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`);
44 });
45
8c7fb565
RBR
46 socket.on('relaySessionDescription', async (data) => {
47 const sockets = await server.in(data.url).fetchSockets()
48 console.log(sockets);
49 sockets.forEach((peer) => {
80172072
RBR
50 peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description))
51 });
ed9f8fd5 52
53 console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`);
54 });
80172072 55});