-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
102 lines (84 loc) · 3.19 KB
/
Copy pathapp.js
File metadata and controls
102 lines (84 loc) · 3.19 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
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
var fs = require('fs')
, util = require('util')
, config = require('./config')
//, key = fs.readFileSync('ssl/server.key', 'utf8')
//, cert = fs.readFileSync('ssl/server.crt', 'utf8')
//, creds = { key: key, cert: cert }
, express = require('express')
, session = require("express-session")({
secret: config.secret
, resave: true
, saveUninitialized: true
, cookie: {
maxAge: 60000
}
})
, app = express()
, http = require('http')
//, https = require('https')
, httpServer = http.createServer(app)
//, httpsServer = https.createServer(creds, app)
// , io = require('socket.io')(httpServer)
, io = require('socket.io').listen(httpServer)
, sharedsession = require("express-socket.io-session")
, UUID = require('node-uuid')
, game = require(config.moduleName)
, assetsPath = util.format('%s/node_modules/%s/assets', __dirname, config.moduleName)
;
app.use(session);
io.use(sharedsession(session, { autoSave: true }));
io.on('connection', function(client) {
var session = client.handshake.session
, clientId = session.clientId || UUID()
, username = session.username || 'guest_' + (Math.floor(Math.random() * 1000))
;
if (!session.clientId) {
client.handshake.session.clientId = clientId;
}
if (!session.username) {
client.handshake.session.username = username;
}
console.log('client %s connected, session: %s', clientId, util.format(session));
client.on('event', function(data) {
var res = false;
if (data.command === 'nick') {
username = data.data.username;
client.handshake.session.username = username;
res = { command: 'namechange', data: { clientId: clientId, username: client.handshake.session.username } }
} else {
res = game.server.onData(data, client.handshake.session);
}
if (res) {
client.emit('event', res);
}
});
client.on('disconnect', function() {
console.log('client disconnected');
});
client.emit('event', { command: 'welcome', data: { clientId: clientId } });
});
app.set('port', (process.env.PORT || 5000));
//app.set('httpsPort', (process.env.HTTPSPORT || 5001));
app.use(express.static(__dirname + '/public'));
app.get('/game/name', function(req, res) { res.status(200).send(game.server.name); });
app.get('/game/description', function(req, res) { res.status(200).send(game.server.description); });
app.get('/game/version', function(req, res) { res.status(200).send(game.server.version); });
// create endpoints for the game screenshots
[].forEach.call(game.server.screenshots, function(screenshot) {
var urlFragment = screenshot.url
, path = util.format('%s/node_modules/%s/%s', __dirname, config.moduleName, screenshot.path)
;
app.get(urlFragment, function(req, res) {
res.status(200).sendFile(path);
});
});
app.use('/game/assets', express.static(assetsPath));
// ---- start the server(s)
httpServer.listen(app.get('port'), function() {
console.log("Serving http on port " + app.get('port'));
});
/*
httpsServer.listen(app.get('httpsPort'), function() {
console.log("Serving https on port " + app.get('httpsPort'));
});
*/