]> git.r.bdr.sh - rbdr/junction/blame_incremental - server/index.js
Update for socket.io 4
[rbdr/junction] / server / index.js
... / ...
CommitLineData
1import { Server } from 'socket.io';
2import { port } from './config.js';
3import * as events from './events.js';
4
5const server = new Server(port);
6console.log(`Listening on port ${port}`);
7
8const sockets = {};
9
10server.on('connection', (socket) => {
11 sockets[socket.id] = socket;
12 const me = socket.id;
13
14 console.log(`[CONNECT] New client connected with ID ${me}`);
15
16 socket.on('join', (data) => {
17 socket.join(data.url);
18 const sockets = server.of(data.url).sockets.keys();
19 sockets.forEach((peerId) => {
20 if (peerId !== me) {
21 const peer = server.sockets.sockets.get(peerId)
22 peer.emit(events.types.ADD_PEER, events.addPeer(me, false));
23 socket.emit(events.types.ADD_PEER, events.addPeer(peerId, true));
24 }
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 => server.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 const sockets = server.of(data.url).sockets.keys();
38 sockets.forEach((peerId) => {
39 const peer = server.sockets.sockets.get(peerId)
40 peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate))
41 });
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 const sockets = server.of(data.url).sockets.keys();
48 sockets.forEach((peerId) => {
49 const peer = server.sockets.sockets.get(peerId)
50 peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description))
51 });
52
53 console.log(`[RELAY_SESSION_DESCRIPTION] Session description for client ${me} has been relayed to all peers`);
54 });
55});