forked from sergi/geekdots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (46 loc) · 1.42 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 fs = require("fs");
var http = require("http");
var server = http.createServer(function(req, res) {
if (req.url.match(/^\/(index.html)?$/)) {
return serve(res, "/www/index.html");
}
else if (req.url.match(/^\/styles.css$/)) {
return serve(res, "/www/styles.css", "text/css");
}
else if (req.url.match(/^\/client.js$/)) {
return serve(res, "/www/client.js", "text/javascript");
}
res.writeHead(404, {"Content-Type": "text/html"});
res.end("Not found");
});
server.listen(process.env.PORT);
var io = require("socket.io").listen(server);
io.set("log level", 2);
io.sockets.on("connection", function(socket) {
var id = socket.id;
socket.on("cursor", function(data) {
data.id = id;
io.sockets.emit("cursor", data);
});
socket.on("click", function() {
io.sockets.emit("click", id);
});
socket.on("disconnect", function() {
io.sockets.emit("remove", id);
});
});
// UTILS
function serve(res, path, mime) {
fs.readFile(__dirname + path, function(err, data) {
if (err)
return error(res, err);
res.writeHead(200, {"Content-Type": mime || "text/html"});
res.end(data);
});
}
function error(res, err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.end("Internal server error: " + err);
console.log(err);
}
console.log(process.env.PORT)