-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (51 loc) · 1.77 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
var app = require('http').createServer(handler),
url = require('url'),
fs = require('fs'),
path = require('path'),
io = require('socket.io').listen(app);
app.listen(8080);
/**
* Serve static html/css/js Files
*/
function handler (req, res) {
var uri = url.parse(req.url).pathname,
filename = path.join(process.cwd(), "client", unescape(uri)),
mimeTypes = { "html": "text/html", "js": "text/javascript", "css": "text/css"},
stats;
try {
stats = fs.lstatSync(filename); // throws if path doesn't exist
} catch (e) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('404 Not Found\n');
res.end();
return;
}
if (stats.isFile()) { // path exists, is a file
var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
res.writeHead(200, {'Content-Type': mimeType} );
var fileStream = fs.createReadStream(filename);
fileStream.pipe(res);
} else { // path is a directory or Symbolic link, other?
res.writeHead(500, {'Content-Type': 'text/plain'});
res.write('500 Internal server error\n');
res.end();
}
}
/**
* Realtime communication for control messages
*/
io.sockets.on('connection', function (socket) {
// first answer
socket.emit('connected', { 'SocketID': socket.id });
socket.on('register', function(){
// TODO: handle register requests from sender and receiver clients to bring them together
});
// control signal handler
socket.on('cs', function (data) {
// TODO: send the data to the appropiate receiver
io.sockets.emit('cs', data);
console.log(data);
});
// disconnet handler
socket.on('disconnect', function(){});
});