-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (42 loc) · 1.2 KB
/
Copy pathindex.js
File metadata and controls
54 lines (42 loc) · 1.2 KB
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
var express = require('express')
var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
var game = require('./game')
var world = require('./world')
var compression = require('compression');
var isLooping = false
app.use(compression({
threshold : 512
}))
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
app.use(express.static(__dirname + '/client'))
server.listen(process.argv[2] || process.env.PORT || 3000)
io.on('connection', function (socket) {
if(isLooping === false) update()
console.log('socket connected.')
socket.on('chat', function (data) {
if(data.msg && data.nick) {
io.emit('chat', {msg: data.msg, nick: data.nick})
}
})
socket.on('addEntity', function (data) {
game.game.addEntity(data.x, data.y)
})
})
function update(){
isLooping = true
game.tick(function(){
var w = world.getParsed();
app.set('tick', w)
});
setTimeout(update, 1000);
}
app.route('/update')
.get(function(req, res) {
res.send(app.get('tick'))
})