-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
112 lines (87 loc) · 2.68 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const cors = require("cors")
const RoomsManager = require("./src/RoomFactory")
const path = require("path")
const express = require("express")
const app = express()
const bodyParser = require('body-parser');
const http = require("http").createServer(app)
const io = require("socket.io")(http, { pingInterval: 5000 })
require("dotenv").config()
io.on("connection", socket => {
if (!socket.handshake.query.id) {
return socket.error("No room id was provided")
}
const room = RoomsManager.getRoom(socket.handshake.query.id)
if (!room) {
return socket.error("Room id provided is invalid")
}
const roomId = room.id.toString()
socket.join(roomId)
const username = socket.handshake.query.username
room.addClient(socket.id, username)
socket.on("PLAY", () => {
room.play(username)
io.to(roomId).emit("STATE", room.state)
})
socket.on("PAUSE", () => {
room.pause(username)
io.to(roomId).emit("STATE", room.state)
})
socket.on("SEEK", (time, diff) => {
room.seek(time, diff, username)
io.to(roomId).emit("STATE", room.state)
})
socket.on("VIDEO", link => {
room.addVideo(link, username)
})
socket.on("SELECT_VIDEO", videoIndex => {
room.userSelectVideo(videoIndex, username)
io.to(roomId).emit("STATE", room.state)
})
socket.on("REMOVE_VIDEO", videoIndex => {
room.removeVideo(videoIndex, username)
io.to(roomId).emit("STATE", room.state)
})
socket.on("SET_SPEED", speed => {
room.setSpeed(speed, username)
io.to(roomId).emit("STATE", room.state)
})
socket.on("PING", (ping, syncDiff) => {
room.updateClient(socket.id, { ping, syncDiff })
})
socket.on("CLIENT_UPDATE", (visibility, muted, volume) => {
room.updateClient(socket.id, { visibility, muted, volume })
})
io.to(roomId).emit("INITIAL_STATE", {
player: room.state,
clients: room.clients,
})
socket.on("disconnect", () => {
room.removeClient(socket.id)
io.to(roomId).emit("CLIENTS_STATE", room.clients)
})
})
RoomsManager.on("create", room => {
room.on("update", () => {
io.to(room.id).emit("STATE", room.state)
})
room.on("time", () => {
io.to(room.id).emit("ELAPSED", room.state.elapsed)
})
room.on("error", err => {
io.to(room.id).emit("ERROR", err)
})
room.on("client", client => {
io.to(room.id).emit("CLIENT", client)
})
})
app.use(cors())
app.use(bodyParser.json())
app.use("/api", require("./src/routes"))
app.use(express.static(path.join(__dirname, "client/build")))
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"))
})
http.listen(process.env.PORT || 4001, () => {
console.log(`Example app listening on port ${http.address().port}`)
})