This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (50 loc) · 2.08 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
60
61
62
63
64
65
66
// server.js
// set up ========================
var express = require('express');
var app = express(); // create our app w/ express
var morgan = require('morgan'); // log requests to the console (express4)
var Game = require('./game.js');
// Realtime comunication, both ways...
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.set('port', (process.env.PORT || 8081));
var game = new Game();
io.on('connection', function (socket) {
var socketId = socket.id
var clientIp = socket.request.connection.remoteAddress
console.log("IO " + socketId + "/" + clientIp);
game.newPlayer(socketId);
socket.on('disconnect', function() {
game.removePlayer(socketId);
});
socket.on('control', function(direction) {
game.changePlayerDirection(socketId, direction);
});
socket.emit('init', game.metadata);
});
game.on('refood', function(food) {
io.emit('food', food);
});
game.on('moved', function(players){
io.emit('plyr', players);
});
// CORS (Necessário para teste do host... :P)
var allowCrossDomain = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
}
// configuration =================
app.use(morgan('dev')); // log every request to the console
app.use(allowCrossDomain);
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
// routes ======================================================================
// application -------------------------------------------------------------
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
// listen (start app with node server.js) ======================================
//app.listen(8080);
server.listen(app.get('port'), function() {
console.log("Node app is running on port:" + app.get('port'))
});