-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
154 lines (124 loc) · 4.76 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, io = require('socket.io')
, useragent = require('useragent')
, gm = require('googlemaps')
// Uncomment to update useragent lookup table
//useragent(true);
///////////////////////////////////////////////////////////////////// ROBODECK APP
var app = module.exports = express.createServer();
// console.log(app);
// Set the state of the slides to 0
// var state = 0;
// Clients is a list of users who have connected
var clients = [];
///////////////////////////////////////////////////////////////////// SEND() UTILITY
function send(message) {
clients.forEach(function(client) {
client.send(message);
});
}
///////////////////////////////////////////////////////////////////// DATE UTILITY
function getTime(){
var currentTime = new Date();
return currentTime;
}
///////////////////////////////////////////////////////////////////// APP CONFIGS
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
///////////////////////////////////////////////////////////////////// INITIAL LOAD - DEVICE DETECT --> SERVE MARKUP, STATIC ASSETS
///////////////////////////////////////////////////////////////////// ROUTES
app.get('/', function(req, res) {
routes.desktop(req, res);
// var ua = useragent.is(req.headers['user-agent'])
// switch(true)
// {
// case ua.chrome:
// console.log('within ROOT route / detected as chrome desktop');
// routes.desktop(req, res);
// break;
// case ua.mobile_safari:
// console.log('within ROOT route / detected as mobile_safari');
// routes.iphone(req, res);
// break;
// default:
// console.log('within ROOT route / fallback to default');
// routes.desktop(req, res);
// }
});
app.get('/x', function(req, res) {
routes.iphone(req, res);
});
///////////////////////////////////////////////////////////////////// ACCEPT XHR CALLS FROM REMOTE MOBILE APP
///////////////////////////////////////////////////////////////////// ROUTES - Next()
app.get('/next', function(req, res) {
console.log('NEXT- ' + 'server time: ' + getTime() + ', client time: ' + req);
send(JSON.stringify({ "cmd": 'next' }));
});
///////////////////////////////////////////////////////////////////// ROUTES - Back()
app.get('/back', function(req, res) {
console.log('PREV ' + getTime());
send(JSON.stringify({ "cmd": 'prev' }));
});
///////////////////////////////////////////////////////////////////// ROUTES - Other()
app.get('/other', function(req, res) {
send(JSON.stringify({ "fn": function(){console.log('other callback fn')} }));
});
app.listen(process.env.PORT || 1511);
///////////////////////////////////////////////////////////////////// SOCKET.IO SERVER
// var sio = io.listen(app, {"heartbeats": false, "transports": ['websocket'], "close timeout": 100});
// var sio = io.listen(app, {"heartbeats": false, "close timeout": 100});
var sio = io.listen(app);
console.log(sio.settings);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
///////////////////////////////////////////////////////////////////// SOCKET.IO WEB SOCKETS
sio.sockets.on('connection', function(client) {
// For each connection made add the client to the array of clients.
console.log('server connection EVENT FIRED');
//console.log('CLIENT connected');
console.log('CLIENT ID: ' + client.id + ', TRANSPORT MECHANISM: ' + sio.transports[client.id].name);
///////////////////////////////////////////////////////////////////// BUILD CLIENTS LIST
clients.push(client);
console.log(clients.length);
send(JSON.stringify({ "clients": clients.length }));
// log each clients id
clients.forEach(function(client) {
console.log('CLIENT ID: ' + client.id);
// console.log(client);
});
client.on('disconnect', function () {
console.log('disconnect EVENT FIRED');
console.log(clients.length)
var index = clients.indexOf(client.id);
console.log(index)
clients.splice(index, 1);
console.log(clients.length)
});
client.on('geo', function(data) {
console.log('geo MESSAGE received');
console.log(data);
var loc = data.lat + "," + data.long
console.log(loc)
gm.reverseGeocode(loc, function(err, data){
var city = data.results[0].address_components[2].long_name;
var state = data.results[0].address_components[4].long_name;
var loc = city + ", " + state;
send(JSON.stringify({ "loc": loc }));
});
});
});