]> git.r.bdr.sh - rbdr/junction/blame - server/index.js
Merge branch 'babel-setup' into 'main'
[rbdr/junction] / server / index.js
CommitLineData
ed9f8fd5 1const PORT = 8000;
2
3const express = require('express');
4const http = require('http');
5const main = express();
6const server = http.createServer(main);
7const io = require('socket.io').listen(server);
8const events = require('./events');
9
10server.listen(PORT, null, function() {
11 console.log("Listening on port " + PORT);
12});
13
14const sockets = {};
15
16io.sockets.on('connection', (socket) => {
17 sockets[socket.id] = socket;
18 const me = socket.id;
19
20 console.log(`[CONNECT] New client connected with ID ${me}`);
21
22 socket.on('join', (data) => {
ad7fccbe 23 Object.keys(io.in(data.url).sockets).forEach(peer => {
24 peer.emit(events.types.ADD_PEER, events.addPeer(me, false));
25 socket.emit(events.types.ADD_PEER, events.addPeer(peer, true));
26 });
ed9f8fd5 27
ad7fccbe 28 socket.join(data.url);
ed9f8fd5 29 console.log(`[CONNECT] Client ${me} added to room ${data.url}`);
30 });
31
32 socket.on('disconnecting', () => {
33 const rooms = Object.keys(socket.rooms);
34 rooms.forEach(room => io.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me)));
35
36 console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`);
37 });
38
39 socket.on('relayICECandidate', (data) => {
40 Object.keys(io.in(data.url).sockets).forEach(peer =>
41 peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate)));
42
43 console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`);
44 });
45
46 socket.on('relaySessionDescription', (data) => {
47 Object.keys(io.in(data.url).sockets).forEach(peer =>
48 peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description)));
49
50 console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`);
51 });
52});