-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
187 lines (152 loc) · 5.79 KB
/
app.js
File metadata and controls
187 lines (152 loc) · 5.79 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
console.clear();
const fs = require("fs");
const path = require("path");
const express = require("express");
const app = express();
const socket = require("socket.io");
const Frame = require("./Frame.js");
const { SSL_OP_CISCO_ANYCONNECT } = require("constants");
const port = 30000;
let server = app.listen(port, () => {
console.log("TheFrame LS03 SynchroServer started on port " + port);
console.log(`Open a browser on http://192.168.145.65:${port}`);
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/index.html"));
});
app.use(express.static('public'));
let io = socket(server);
let masterSocket = null;
let frames = [], ips = [];
let syncTime;
fs.readFile("./frames.json", "utf8", (err, data) => {
if(err) return console.log(`ERROR: ${err}`);
ips = JSON.parse(data.toString());
});
let videos = [];
fs.readFile('./videos.json', "utf8", (err, data) => {
if(err) return console.log(`ERROR: ${err}`);
videos = JSON.parse(data.toString());
});
io.sockets.on('connection', (socket) => {
let frame = new Frame(socket.request.connection._peername.address.replace("::ffff:", ""), socket.id);
frames.push({ 'socket': socket, 'frame': frame });
ips.find(o => {
if(o.ip === frame.getIp()) {
frame.setId(o.name);
}
});
console.log(`Frame connected -> ${frame.getId()}`);
if(masterSocket === null) {
masterSocket = socket;
socket.emit('master');
socket.emit('message', `time=0`);
socket.emit('message', `videoState=play`);
console.log(`Master changed -> ${frame.getId()}`);
} else {
socket.emit('message', `time=${syncTime + 0.63}`);
socket.emit('message', `videoState=play`);
}
socket.on('sync', (currentTime) => {
syncTime = currentTime;
});
socket.on('disconnect', () => {
console.log(`Frame disconnected -> ${frame.getId()}`);
frames.splice(frames.indexOf({ 'socket': socket, 'frame': frame }), 1);
if(socket === masterSocket) {
if(frames.length > 0) {
masterSocket = frames[frames.length - 1];
masterSocket.emit('master');
console.log(`Master changed -> ${frame.getId()}`);
} else {
masterSocket = null;
console.log(`master=null`);
}
}
});
});
app.get("/video", (req, res) => {
const frame = frames.find(o => o.frame.getIp() == req.connection.remoteAddress.replace("::ffff:", ""));
const videoToStream = videos.find(o => o.name == frame.frame.getId());
const path = `./${videoToStream.video}`;
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = req.headers.range;
if(range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1]
? parseInt(parts[1], 10) : fileSize - 1;
if(start >= fileSize) {
res.status(416).send(`Requested range not satisfiable \n${start} >= ${fileSize}`);
return;
}
const chunksize = (end - start) + 1;
const file = fs.createReadStream(path, {start, end});
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4'
};
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4'
};
res.writeHead(200, head);
fs.createReadStream(path).pipe(res);
}
});
const stdin = process.openStdin();
stdin.addListener('data', (data) => {
const args = data.toString().replace("\r", "").replace("\n", "").toLowerCase().split(" ");
if(args[0] == null || args[0] == "") return;
if(args[0] === "sync") {
if(masterSocket != null) {
console.log(`Synchronizing all frames from current given sync time (master=${frames.find(frame => frame.socket === masterSocket).frame.getId()})`);
frames.forEach(frame => {
if(frame.socket != masterSocket) {
frame.socket.emit('message', `time=${syncTime + 0.63}`);
}
});
} else {
console.log(`No master connection is active`);
}
} else if(args[0] == "reconnect") {
if(frames.length > 0) {
frames.forEach(frame => {
frame.socket.emit('neuverbinden');
});
} else {
console.log(`No active connections to reconnect!`);
}
} else if(args[0] == "master") {
if(args.length > 1) {
if(frames.length === 1) {
return console.log("Currently there is only 1 frame connected!");
} else {
let frame = frames[frames.length - 1];
masterSocket = frame.socket;
masterSocket.emit('master');
console.log(`The new master is ${frame.frame.getId()}`);
}
} else {
if(masterSocket != null) {
console.log(`Currently the master is ${frames.find(frame => frame.socket === masterSocket).frame.getId()}`);
} else {
console.log("There is no master connected!");
}
}
} else if(args[0] == "stop") {
console.log("Stopping...");
frames.forEach(frame => {
frame.socket.emit('exit', 0);
});
process.exit(0);
} else {
console.log(`Couldn't find command '${args[0]}'`);
}
});