-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
50 lines (40 loc) · 1.17 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
const ws = require('websocket').server;
const http = require('http');
const fs = require('fs');
const path = require('path');
const port = 8081;
const indexFile = path.resolve(process.cwd(), './remote/index.html');
const jsFile = path.resolve(process.cwd(), './remote/dreamremote.js');
const clients = new Set();
const server = http.createServer((request, response)=>{
const file = request.url.indexOf('.js') > 0 ? jsFile : indexFile;
if(request.url.indexOf('fav') > 0) {
response.writeHead(404);
response.end();
return;
}
fs.readFile(file, 'binary', (err, file) => {
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
const socket = new ws({
httpServer: server
});
server.listen(port, ()=>{
console.log(`Dream-remote is listening on port ${port}`);
});
socket.on('request', function(request) {
const connection = request.accept(null, request.origin);
clients.add(connection);
connection.on('message', function(message) {
for(let c of clients) {
//if(message.utfData)
c.sendUTF(message.utf8Data);
}
});
connection.on('close', function(connection) {
clients.delete(connection);
});
});