-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
83 lines (74 loc) · 1.8 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
83
var http = require('http')
, app = http.createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, url = require('url')
, clients = {}
, clientsReverse = {}
, controls = {}
, controlsReverse = {}
app.listen(8080);
function handler (req, res) {
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
var pathname = url_parts.pathname;
if(pathname == '/') {
ouput_html(res, "index.html");
} else if(pathname == '/remote.html'){
ouput_html(res, "remote.html");
} else {
res.writeHead(404);
res.end();
}
}
function ouput_html(res, filename) {
fs.readFile(__dirname + '/' + filename,
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead("Content-Type", "text/html")
res.writeHead(200);
res.end(data);
}
);
}
var randomID= function() {
var from = 1000;
var to = 9999;
return Math.floor(Math.random()*(to-from+1)+from);
}
io.sockets.on("connection", function(socket){
socket.on("screen_connect", function(data, fn){
var id = data.screen_id;
if(id in clients){
if(! (id in controlsReverse)){
controlsReverse[id] = socket.id;
controls[socket.id] = id;
fn(true);
} else {
console.log("#######")
fn(false);
}
} else {
console.log("*******")
fn(false);
}
});
socket.on("get_id", function(fn){
var id = randomID();
while(id in clients) {
id = randomID();
}
clients[id] = true
socket.join(id);
fn(id);
});
socket.on("msg", function(data){
var id = controls[socket.id]
if(clients[id] != null || clients[id] != 'undefined') {
io.sockets.in(data.screenId).emit("msg", data);
}
});
});