]>
git.r.bdr.sh - rbdr/junction/blob - server/index.js
1 import { Server
} from "socket.io";
2 import { port
} from "./config.js";
3 import * as events
from "./events.js";
5 const server
= new Server(port
);
6 console
.debug(`Listening on port ${port}`);
10 server
.on(events
.types
.CONNECTION
, (socket
) => {
13 console
.debug(`[CONNECT] New client connected with ID ${me}`);
15 socket
.on(events
.types
.JOIN
, async ({ room
}) => {
22 rooms
[room
].push(socket
.id
);
24 const sockets
= await server
.in(room
).fetchSockets();
25 sockets
.forEach((peer
) => {
27 events
.addPeer(me
, false)(peer
);
28 events
.addPeer(peer
.id
, true)(socket
);
33 socket
.on(events
.types
.DISCONNECTING
, () => {
34 for (const room
in rooms
) {
35 if (rooms
[room
].includes(me
)) {
36 rooms
[room
] = rooms
[room
].filter((id
) => id
!== me
);
37 events
.removePeer(me
)(socket
.to(room
));
38 if (rooms
[room
].length
=== 0) {
45 socket
.on(events
.types
.RELAY_ICE_CANDIDATE
, async ({ candidate
, peerId
}) => {
46 events
.ICECandidateReceived(me
, candidate
)(socket
.to(peerId
));
49 socket
.on(events
.types
.RELAY_OFFER
, async ({ offer
, peerId
}) => {
50 events
.offerReceived(me
, offer
)(socket
.to(peerId
));
53 socket
.on(events
.types
.RELAY_ANSWER
, async ({ answer
, peerId
}) => {
54 events
.answerReceived(me
, answer
)(socket
.to(peerId
));