-
Notifications
You must be signed in to change notification settings - Fork 63
/
server.js
49 lines (41 loc) · 1.24 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
//TODO: rename as "blockly.io".
var hostname = process.argv[2] || "0.0.0.0";
var port = parseInt(process.argv[3]) || 80;
var finalhandler = require('finalhandler');
var http = require('http');
var serveStatic = require('serve-static');
// Serve up public/ftp folder
var serve = serveStatic('public/', {'setHeaders': setHeaders});
function setHeaders(res, path) {
res.setHeader('Cache-Control', 'public, max-age=0');
}
// Create server
var server = http.createServer(function onRequest (req, res) {
serve(req, res, finalhandler(req, res));
});
// Listen
server.listen(port, hostname);
server = http.createServer();
var io = require('socket.io')(server);
io.set('transports', ['websocket']);
var Game = require('./game-server.js');
var games = [new Game()];
io.on('connection', function(socket){
socket.on("hello", function(data, fn) {
//TODO: error checking.
if (data.name && data.name.length > 32)
fn(false, "Your name is too long!");
else if (!games[0].addPlayer(socket, data.name))
fn(false, "Game is too full!");
else
fn(true);
});
socket.on("checkConn", function(fn) { fn(); });
});
server.listen(8081);
function tick() {
games[0].tickFrame();
setTimeout(tick, 1000 / 60);
}
tick();
//setTimeout(tick, 1000 / 60);