-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (64 loc) · 2.42 KB
/
server.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
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: true });
const roomToUsersMap = {};
io.on("connection", (socket) => {
socket.on("message", (message) => {
io.emit("message", message);
});
socket.on('newDate', (data) => {
socket.broadcast.emit('newDate', data);
})
socket.on('notification', async (data) => {
io.emit('notification', data);
})
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on("room:join", (data) => {
const { email, room, peerId } = data;
socket.join(room);
if (!roomToUsersMap[room]) {
roomToUsersMap[room] = [{ email, peerId }];
} else {
const userIndex = roomToUsersMap[room].findIndex(user => user.email === email);
if (userIndex === -1) {
roomToUsersMap[room].push({ email, peerId });
} else {
roomToUsersMap[room][userIndex].peerId = peerId;
}
}
io.to(room).emit("room:users", roomToUsersMap[room]);
});
socket.on('room:leave', (data) => {
const { email, room } = data;
socket.leave(room);
roomToUsersMap[room] = roomToUsersMap[room].filter((user) => user.email !== email);
io.to(room).emit("room:users", roomToUsersMap[room]);
});
socket.on('room:user:toggle:audio', (data) => {
const { peerId, email, room } = data;
io.to(room).emit("room:user:toggle:audio", { peerId, email });
});
socket.on('room:user:toggle:video', (data) => {
const { peerId, email, room } = data;
io.to(room).emit("room:user:toggle:video", { peerId, email });
});
});
const server = httpServer
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});