-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (42 loc) · 1.09 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
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var io = require('socket.io');
var mime = require('mime');
var httpServer = http.createServer(function(req,res) {
var cacheLoad;
if(req.url == "/" || req.url == "") {
cacheLoad = "./www/index.html";
}
else {
cacheLoad = "./www" + url.parse(req.url).pathname;
}
var httpStatusCode = 200;
fs.exists(cacheLoad,function(statusCheck) {
if(!statusCheck) {
httpStatusCode = 404;
cacheLoad = "./www/index.html";
}
var cache = fs.readFileSync(cacheLoad);
var mimeType = mime.lookup(cacheLoad);
res.writeHead(httpStatusCode,{'Content-type':mimeType});
res.end(cache);
});
})
httpServer.listen(8000);
var webSocket = io.listen(httpServer);
webSocket.sockets.on('connection',function(socket){
socket.on('start',function(x,y) {
socket.broadcast.emit('start',x,y);
});
socket.on('move',function(x,y) {
socket.broadcast.emit('move',x,y);
});
socket.on('stop',function() {
socket.broadcast.emit('stop');
});
socket.on('clear',function() {
socket.broadcast.emit('clear');
});
});