-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
141 lines (121 loc) · 3.74 KB
/
server.js
File metadata and controls
141 lines (121 loc) · 3.74 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
// server roads
app.use("/assets", express.static("assets"));
app.get('/', function(req, res) {
res.sendFile(__dirname + "/index.html");
})
app.get('/remote.html', function(req, res) {
res.sendFile(__dirname + "/assets/views/remote.html");
})
app.get('/readme.md', function(req, res) {
res.sendFile(__dirname + "/assets/views/readme.md");
})
var roomList=[];
// on connection
io.on('connection', function(socket) {
// on room request
socket.on("roomRequest", function(room) {
// if the room dosent exist and not null
if(!checkRoom(room) && room != null ) {
// say yes
socket.emit("roomAnswer",true);
// add the room
roomList.push(room);
// save the room into the socket object and join it
socket.join(room);
socket.roomIn = room;
// save type of socket
socket.type = "desktop";
}
else {
// sayno
socket.emit("roomAnswer",false);
}
})
// on remote request
socket.on("remoteRequest", function(room) {
// if room exist
if(checkRoom(room)) {
// say yes
socket.emit("remoteAnswer",true);
//join and save the room
socket.join(room);
socket.roomIn = room;
// save type
socket.type = "remote";
}
else {
// say no
socket.emit("remoteAnswer",false);
}
})
// when remote is ready
socket.on("remoteReady", function() {
io.to(socket.roomIn).emit("remoteReady");
})
// when database is sent
socket.on("addVideos", function(tab, index) {
io.to(socket.roomIn).emit("addVideos", tab, index);
})
socket.on("updateIndex", function(index) {
io.to(socket.roomIn).emit("updateIndex", index);
})
// events
socket.on("togglePlay", function() {
io.to(socket.roomIn).emit("togglePlay");
})
socket.on("videoPlay", function() {
io.to(socket.roomIn).emit("videoPlay");
})
socket.on("videoPause", function() {
io.to(socket.roomIn).emit("videoPause");
})
socket.on("autoMode", function() {
io.to(socket.roomIn).emit("autoMode");
})
socket.on("autoModeStatus", function(status) {
io.to(socket.roomIn).emit("autoModeStatus", status);
})
socket.on("goTime", function(direction) {
io.to(socket.roomIn).emit("goTime",direction);
})
socket.on("playlistUpdate", function(direction) {
io.to(socket.roomIn).emit("playlistUpdate", direction);
})
socket.on("soundUpdate", function(direction) {
io.to(socket.roomIn).emit("soundUpdate", direction);
})
socket.on("mute", function() {
io.to(socket.roomIn).emit("mute");
})
socket.on("changeVideo",function(index){
io.to(socket.roomIn).emit("changeVideo", index);
})
// on disconnect
socket.on("disconnect", function() {
// if the room is in the tab and the disconnect socket is a desktop
if(roomList.indexOf(socket.roomIn) !=-1 && socket.type=="desktop") {
roomList.splice(roomList.indexOf(socket.roomIn),1);
io.to(socket.roomIn).emit("disconnectP");
}
// if this is a remote
else {
io.to(socket.roomIn).emit("disconnectR");
}
})
});
// check if room is in the tab
function checkRoom(room) {
for(i =0; i<roomList.length; i++) {
if(room == roomList[i]) {
return true;
}
}
return false;
}
// server listen on 8080 port
console.log("listen on 8080 port")
http.listen(8080);