]> git.r.bdr.sh - rbdr/junction/blame_incremental - server/index.js
Merge branch 'server' into 'main'
[rbdr/junction] / server / index.js
... / ...
CommitLineData
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) => {
23 socket.join(data.url);
24 Object.keys(io.in(data.url).sockets).forEach(peer => peer.emit(events.types.ADD_PEER, events.addPeer(me, peer.id === me)));
25
26 console.log(`[CONNECT] Client ${me} added to room ${data.url}`);
27 });
28
29 socket.on('disconnecting', () => {
30 const rooms = Object.keys(socket.rooms);
31 rooms.forEach(room => io.to(room).emit(events.types.REMOVE_PEER, events.removePeer(me)));
32
33 console.log(`[DISCONNECT] Client ${me} has disconnected and has been removed from all rooms`);
34 });
35
36 socket.on('relayICECandidate', (data) => {
37 Object.keys(io.in(data.url).sockets).forEach(peer =>
38 peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate)));
39
40 console.log(`[RELAY_ICE_CANDIDATE] ICE candidate for client ${me} has been relayed to all peers`);
41 });
42
43 socket.on('relaySessionDescription', (data) => {
44 Object.keys(io.in(data.url).sockets).forEach(peer =>
45 peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description)));
46
47 console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`);
48 });
49});