forked from christen03/LAHacksNext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
58 lines (49 loc) · 1.56 KB
/
server.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { createServer } from "node:http";
import next from "next";
import { Server } from "socket.io";
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = 3000;
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
app.prepare().then(() => {
const httpServer = createServer(handler);
const io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("disconnect", () => {
console.log("user disconnected");
const rooms = Array.from(socket.rooms);
rooms.forEach(room => {
if (room !== socket.id) {
socket.to(room).emit('userLeft', socket.id);
}
});
});
socket.on('signal', ({ to, signalData, from }) => {
console.log("recieved signal signal from", from, "to", to)
io.to(to).emit('receiving signal', { signalData, from });
});
socket.on('joinRoom', ({ roomcode, userId }) => {
console.log("joinRoom", roomcode, "by user", userId);
socket.join(roomcode);
socket.to(roomcode).emit('userJoined', { socketId: socket.id, userId});
io.in(roomcode).allSockets().then(sockets => {
console.log(`All clients in room ${roomcode}:`, Array.from(sockets));
});
});
});
httpServer
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});