-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (68 loc) · 2.04 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
71
72
73
74
75
76
77
78
79
80
81
82
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const cors = require("cors");
const superheroes = require("superheroes");
const io = new Server(server, {
cors: {
origin: "http://localhost:3000",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
},
});
io.on("connection", socket => {
const username = superheroes.random();
const users = {};
users[socket.id] = username;
console.log(`User ${users[socket.id]} connected`);
socket.on("disconnect", () => {
console.log(`User ${users[socket.id]} disconnected`);
});
socket.on("send-url", (id, room) => {
io.to(room).emit("recv-url", id);
});
socket.on("send-data", (data, room) => {
// const clients = io.sockets.adapter.rooms.get(room);
// const first = [...clients][0];
// console.log(first);
// if (first === socket.id)
io.to(room).emit("recv-data", data);
});
socket.on("send-state", (state, room) => {
io.to(room).emit("recv-state", state);
});
socket.on("send-seek", (seek, room) => {
io.to(room).emit("recv-seek", seek);
});
socket.on("send-rewind", (rewind, room) => {
io.to(room).emit("recv-rewind", rewind);
});
socket.on("send-forward", (forward, room) => {
io.to(room).emit("recv-forward", forward);
});
socket.on("send-rate", (rate, room) => {
io.to(room).emit("recv-rate", rate);
});
// socket.on("send-video-link", (link, room) => {
// socket.broadcast.to(room).emit("recv-video-link", link);
// });
socket.on("join-room", room => {
socket.join(room);
console.log(`User ${users[socket.id]} joined ${room}`);
const clients = io.sockets.adapter.rooms.get(room);
if (clients.size > 1) {
console.log(`Users in ${room}: ${clients.size}`);
console.log(clients);
console.log("\n");
}
});
socket.on("leave-room", room => {
socket.leave(room);
console.log(`User ${users[socket.id]} left ${room}`);
});
});
server.listen(process.env.PORT || 5000, () => {
console.log("Server is running on port 5000");
});