This repository has been archived by the owner on Nov 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·69 lines (57 loc) · 2.11 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
67
68
69
/**
* Pluto - Websocket Remote for Node.js
* Eugen Pirogoff
* web: http://www.eugenpirogoff.de
* mail: eugenpirogoff@me.com
**/
// Loading express
express = require('express.io');
app = express().http().io();
app.configure(function() {
app.use(express["static"](__dirname + '/public'));
app.set('rootDir', __dirname);
});
app.enable('browser client minification'); // send minified client
app.enable('browser client gzip'); // gzip the file
app.enable('browser client etag'); // apply etag caching logic based on version number
app.disable('hearbeats');
//WebSocket Sessions JSON
//Connect Controller only to a running Pluto Game Session and Route
var sessions = {}
// routes with parameters passing
app.get('/game/:pluto_pin', function(req, res){
sessions[req.params.pluto_pin] = false;
res.sendfile(__dirname + '/public/game/index.html')
});
// router for pluto controller and the assigned pin
app.get('/controller/:pluto_pin', function(req, res){
if (sessions[req.params.pluto_pin] == false){
res.sendfile(__dirname + '/public/controller/index.html')
sessions[req.params.pluto_pin] = true;
}
else {
//res.sendfile(__dirname + '/public/error.html');
res.send("there is no game awaiting connection to that controller : " + req.params.pluto_pin + "<br>" + "please load a game first and then the controller");
}
});
// listenening for pluto_data event on socket.io and routing the data to the session
app.io.route('pluto_data', function(req){
if (sessions[req.data['controller_session']] == true){
req.io.room(req.data['controller_session']).broadcast('pluto_relay', req.data);
}
})
// pluto connection joins socket.io
app.io.route('pluto_join', function(req){
req.io.join(req.data);
console.log( new Date() + " - Session : " + req.data);
console.log( req.handshake.address);
})
// pluto is leaving socket.io
app.io.route('pluto_leave', function(req){
req.io.leave(req.data);
})
// log current websocket sessions on server
// setInterval(function(){
// console.log("Pluto: concurrent WebSocket connections : " + Object.keys(sessions).length);
// },5000);
app.listen(process.env.VCAP_APP_PORT || 3000);