-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathapp.js
134 lines (91 loc) · 4.91 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Copyright 2012-2016 Sven "underscorediscovery" Bergström
written by : http://underscorediscovery.ca
written for : http://buildnewgames.com/real-time-multiplayer/
MIT Licensed.
Usage : node app.js
*/
var
gameport = process.env.PORT || 4004,
io = require('socket.io'),
express = require('express'),
UUID = require('node-uuid'),
verbose = false,
http = require('http'),
app = express(),
server = http.createServer(app);
/* Express server set up. */
//The express server handles passing our content to the browser,
//As well as routing users where they need to go. This example is bare bones
//and will serve any file the user requests from the root of your web server (where you launch the script from)
//so keep this in mind - this is not a production script but a development teaching tool.
//Tell the server to listen for incoming connections
server.listen(gameport)
//Log something so we know that it succeeded.
console.log('\t :: Express :: Listening on port ' + gameport );
//By default, we forward the / path to index.html automatically.
app.get( '/', function( req, res ){
console.log('trying to load %s', __dirname + '/index.html');
res.sendfile( '/index.html' , { root:__dirname });
});
//This handler will listen for requests on /*, any file from the root of our server.
//See expressjs documentation for more info on routing.
app.get( '/*' , function( req, res, next ) {
//This is the current file they have requested
var file = req.params[0];
//For debugging, we can track what files are requested.
if(verbose) console.log('\t :: Express :: file requested : ' + file);
//Send the requesting client the file.
res.sendfile( __dirname + '/' + file );
}); //app.get *
/* Socket.IO server set up. */
//Express and socket.io can work together to serve the socket.io client files for you.
//This way, when the client requests '/socket.io/' files, socket.io determines what the client needs.
//Create a socket.io instance using our express server
var sio = io.listen(server);
//Configure the socket.io connection settings.
//See http://socket.io/
sio.configure(function (){
sio.set('log level', 0);
sio.set('authorization', function (handshakeData, callback) {
callback(null, true); // error first callback style
});
});
//Enter the game server code. The game server handles
//client connections looking for a game, creating games,
//leaving games, joining games and ending games when they leave.
game_server = require('./game.server.js');
//Socket.io will call this function when a client connects,
//So we can send that client looking for a game to play,
//as well as give that client a unique ID to use so we can
//maintain the list if players.
sio.sockets.on('connection', function (client) {
//Generate a new UUID, looks something like
//5b2ca132-64bd-4513-99da-90e838ca47d1
//and store this on their socket/connection
client.userid = UUID();
//tell the player they connected, giving them their id
client.emit('onconnected', { id: client.userid } );
//now we can find them a game to play with someone.
//if no game exists with someone waiting, they create one and wait.
game_server.findGame(client);
//Useful to know when someone connects
console.log('\t socket.io:: player ' + client.userid + ' connected');
//Now we want to handle some of the messages that clients will send.
//They send messages here, and we send them to the game_server to handle.
client.on('message', function(m) {
game_server.onMessage(client, m);
}); //client.on message
//When this client disconnects, we want to tell the game server
//about that as well, so it can remove them from the game they are
//in, and make sure the other player knows that they left and so on.
client.on('disconnect', function () {
//Useful to know when soomeone disconnects
console.log('\t socket.io:: client disconnected ' + client.userid + ' ' + client.game_id);
//If the client was in a game, set by game_server.findGame,
//we can tell the game server to update that game state.
if(client.game && client.game.id) {
//player leaving a game should destroy that game
game_server.endGame(client.game.id, client.userid);
} //client.game_id
}); //client.on disconnect
}); //sio.sockets.on connection