forked from zackster/CompassionPit--Node-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·177 lines (148 loc) · 4.99 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*jshint devel: true */
(function () {
"use strict";
var express = require("express");
var app = module.exports = express.createServer(),
sys = require("sys"),
nowjs = require("now"),
connect = require("connect"),
Room = require("./rooms/models").Room,
guid = require("./utils").guid,
config = require("./config"),
log = require("./log"),
mongo = require("mongodb");
app.dynamicHelpers({
base: function () {
// return the app's mount-point
// so that urls can adjust. For example
// if you run this example /post/add works
// however if you run the mounting example
// it adjusts to /blog/post/add
return '/' === app.route ? '' : app.route;
}
});
app.configure(function () {
app.use(express.static(__dirname + '/static'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.get("/", function (req, res, next) {
connect.static.send(req, res, next, {
path: "static/index.html"
});
});
app.get('/messageChart', function(req, res){
var mongodb = require('mongodb');
var mongoServer = new mongodb.Server(config.mongodb.host, config.mongodb.port, {});
var mongoDb = new mongodb.Db(config.mongodb.logDb, mongoServer, {});
mongoDb.open(function (error, client) {
var collection = new mongodb.Collection(client, config.mongodb.logCollection);
var messageJSON;
res.escapeMarkup = false;
collection.find({}).toArray(function(err, docs) {
messageJSON = docs;
res.render('messageChart', {
messageJSON: JSON.stringify(messageJSON)
});
});
});
});
require("./rooms/actions")(app);
log.addActions(app);
if (!module.parent) {
require('sys').puts("Server started on port " + config.port);
app.listen(config.port);
}
var everyone = nowjs.initialize(app, {
port: config.nowjsPort,
host: config.nowjsHost
});
var clientIdToRoomId = Object.create(null);
everyone.now.sendMessage = function (message, callback) {
var clientId = this.user.clientId;
var roomId = clientIdToRoomId[clientId];
var room = roomId && Room.get(roomId);
if (!room) {
if (callback) {
callback(null);
}
return;
}
log.store({"action": "messageSent", "time": Date.now().toString()});
room.send(message, clientId, callback || function () {});
};
everyone.now.join = function (type, callback) {
var opposite;
if (type === "venter") {
opposite = "listener";
} else {
type = "listener";
opposite = "venter";
}
var clientId = this.user.clientId;
// disconnect from old room if rejoining
var oldRoomId = clientIdToRoomId[clientId];
var room;
if (oldRoomId) {
room = Room.get(oldRoomId);
if (room) {
room.removeUser(clientId);
}
}
delete clientIdToRoomId[clientId];
room = Room.findOrCreate(type, oldRoomId);
clientIdToRoomId[clientId] = room.id;
room.addUser(clientId, type);
callback(true);
};
everyone.now.ping = function (callback) {
var clientId = this.user.clientId;
var roomId = clientIdToRoomId[clientId];
var room;
if (roomId) {
room = Room.get(roomId);
if (room) {
room.poke(clientId);
}
}
if (callback) {
callback("pong");
}
};
everyone.now.getQueuePosition = function (callback) {
var clientId = this.user.clientId;
var roomId = clientIdToRoomId[clientId];
var room;
if (roomId) {
room = Room.get(roomId);
if (room) {
room.poke(clientId);
}
}
callback(room.getQueuePosition(clientId));
};
everyone.disconnected(function () {
var clientId = this.user.clientId;
var roomId = clientIdToRoomId[clientId];
if (roomId) {
delete clientIdToRoomId[clientId];
var room = Room.get(roomId);
if (room) {
room.removeUser(clientId);
}
}
log.info({
event: "Disconnected",
client: clientId,
room: roomId || null
});
});
process.on('uncaughtException', function (err) {
log.error({
event: "Uncaught exception",
error: String(err.message),
stack: String(err.stack)
});
});
}());