-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
115 lines (85 loc) · 3.21 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
var express = require('express'),
app = express.createServer(),
io = require('socket.io').listen(app),
redis = require("redis"),
jqtpl = require('jqtpl'),
trimet = require('./lib/trimet.js');
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.set('views', __dirname + '/views');
app.set("view engine", "html");
app.set("view options", {
layout: false
});
app.register(".html", jqtpl.express);
});
app.configure('development', function(){
app.use(express.static(__dirname + '/static'));
app.use(express.logger());
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
client = redis.createClient();
speaker = redis.createClient();
});
app.configure('production', function(){
var oneYear = 31557600000;
app.use(express.static(__dirname + '/static', { maxAge: oneYear }));
app.use(express.errorHandler());
app.use(express.logger());
client = redis.createClient(process.env.redis_port, process.env.redis_host);
speaker = redis.createClient(process.env.redis_port, process.env.redis_host);
client.auth(process.env.redis_password, redis.print);
speaker.auth(process.env.redis_password, redis.print);
});
app.get('/', function(req, res) {
console.log('getting /');
res.render('index.html', {});
});
io.sockets.on('connection', function (socket) {
// create a listener for this connection
// this is a redis client that only subscribes to channels
var listener = redis.createClient(process.env.redis_port, process.env.redis_host);
listener.auth(process.env.redis_password, redis.print);
socket.on('get-route', function (route_id, cb) {
if (typeof route_id !== 'number') {
cb('error: get-route#invalid argument');
} else {
trimet.getRouteByID(route_id, function(err, data) {
cb(data);
});
}
});
socket.on('get-routes', function (lat, lon, cb) {
if (typeof lat !== 'number' || typeof lat !== 'number') {
cb('error: get-routes#invalid arguments');
} else {
trimet.getRouteByPoint(lat, lon, function(err, data) {
console.dir(data);
cb(data);
});
}
});
socket.on('submit-comment', function (data) {
socket.get('identity', function (err, identity) {
var comment = identity.nickname + ': ' + data.comment;
if (err) { console.dir(err);}
client.lpush(identity.route_id, JSON.stringify(
{'ts': new Date().getTime(), 'text': comment}));
speaker.publish(identity.route_id, JSON.stringify({'ts': new Date().getTime(), 'text': comment}));
});
});
socket.on('join-channel', function (route_id, nickname) {
var joinMessage = nickname + ' has joined the chat';
console.log('joining channel');
listener.subscribe(route_id);
speaker.publish(route_id, JSON.stringify({'ts': new Date().getTime(), 'text': joinMessage}));
socket.set('identity', {route_id: route_id, nickname: nickname}, function () {});
});
listener.on("message", function (channel, data) {
console.log('listener!');
console.log('got a route comment on ' + channel);
io.sockets.emit('new-route-message', JSON.parse(data));
});
});
app.listen(8000);