-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·59 lines (45 loc) · 1.68 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
var config = require('./config');
// use child_process for physics calculations
var child = require('child_process');
var numCPUs = require('os').cpus().length;
// child_process config
console.log('CPUs:', numCPUs);
// polyfill for nodejitsu compatibility
/*
child.fork = function(file) {
return this.spawn(process.execPath, [file], {
stdio: ['pipe', 1, 2, 'ipc'],
env: process.env
});
}
*/
var worker = child.fork(__dirname + '/game/core/worker.js');
console.log('Worker', worker.pid, worker.connected);
// console.log('worker.send', worker.send);
worker.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died');
});
// server config
var app = require('http').createServer(handler);
app.listen(config.port);
console.log('Server started, listening on port ' + config.port);
// http config
var static = require('node-static');
// add custom mime types
// static.mime.addContentType('svgz', 'application/svg+xml');
// static.mime.addContentType('woff', 'application/font-woff');
var file = new (static.Server)('./dist');
// require server game modules, init using dependency injection if required
// var channel = require('./game/server/redis').init();
var socket = require('./game/server/socket').init(app, worker); //, channel);
// init expire loop
var levels = require('./game/server/levels.js').init(worker).load(worker);
var expire = require('./game/server/expire.js').init(worker); //channel.store);
// require server loops
var input = require('./game/server/input').init(worker);
var update = require('./game/server/update').init(socket);
function handler (req, res) {
req.addListener('end', function () {
file.serve(req, res);
});
}