-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·45 lines (34 loc) · 986 Bytes
/
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
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 = "./404.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('addToMap',function(latitude,longitude) {
socket.emit('meOnMap',latitude,longitude);
socket.broadcast.emit('otherOnMap',latitude,longitude);
});
});