-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.js
411 lines (410 loc) · 11.9 KB
/
client.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
var CONFIG, Util, addMessage, first_poll, longPoll, nicks, onConnect, outputUsers, rss, scrollDown, send, showChat, showConnect, showLoad, starttime, transmission_errors, updateRSS, updateTitle, updateUptime, updateUsersLink, userJoin, userPart, util, who;
var __hasProp = Object.prototype.hasOwnProperty;
CONFIG = {
debug: false,
nick: "#",
// set in onConnect
id: null,
// set in onConnect
last_message_time: 1,
focus: true,
//event listeners bound in onConnect
unread: 0
//updated in the message-processing loop
};
nicks = [];
Date.prototype.toRelativeTime = function(now_threshold) {
var _a, conversions, delta, key, units, value;
delta = new Date() - this;
now_threshold = parseInt(now_threshold, 10);
if (isNaN(now_threshold)) {
now_threshold = 0;
}
if (delta <= now_threshold) {
return 'Just now';
}
units = null;
conversions = {
millisecond: 1,
// ms -> ms
second: 1000,
// ms -> sec
minute: 60,
// sec -> min
hour: 60,
// min -> hour
day: 24,
// hour -> day
month: 30,
// day -> month (roughly)
year: 12
// month -> year
};
_a = conversions;
for (key in _a) { if (__hasProp.call(_a, key)) {
value = _a[key];
if (delta < value) {
break;
} else {
units = key;
delta = delta / value;
}
}}
delta = Math.floor(delta);
if (delta !== 1) {
units += 's';
}
return [delta, units].join(" ");
};
Date.fromString = function(str) {
return new Date(Date.parse(str));
};
//updates the users link to reflect the number of active users
updateUsersLink = function() {
var t;
t = nicks.length.toString() + " user";
if (nicks.length !== 1) {
t += "s";
}
return $("#usersLink").text(t);
};
//handles another person joining chat
userJoin = function(nick, timestamp) {
//put it in the stream
addMessage(nick, "joined", timestamp, "join");
if (nicks.indexOf(nick) === -1) {
//otherwise, add the user to the list
nicks.push(nick);
//update the UI
return updateUsersLink();
}
};
//handles someone leaving
userPart = function(nick, timestamp) {
//put it in the stream
addMessage(nick, "left", timestamp, "part");
//remove the user from the list
nicks.split(nicks.indexOf(nick), 1);
//update the UI
return updateUsersLink();
};
// utility functions
Util = function() {};
Util.prototype.urlRE = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([^\s]*(\?\S+)?)?)?/g;
Util.prototype.toStaticHTML = function(inputHtml) {
inputHtml = inputHtml.toString();
return inputHtml.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
};
Util.prototype.zeroPad = function(digits, n) {
n = n.toString();
while (n.length < digits) {
n = '0' + n;
}
return n;
};
Util.prototype.timeString = function(date) {
var hours, minutes;
minutes = date.getMinutes().toString();
hours = date.getHours().toString();
return this.zeroPad(2, hours) + ":" + this.zeroPad(2, minutes);
};
Util.prototype.isBlank = function(text) {
var blank;
blank = /^\s*$/;
return text.match(blank) !== null;
};
util = new Util();
//used to keep the most recent messages visible
scrollDown = function() {
window.scrollBy(0, 100000000000000000);
return $("#entry").focus();
};
//inserts an event into the stream for display
//the event may be a msg, join or part type
//from is the user, text is the body and time is the timestamp, defaulting to now
//_class is a css class to apply to the message, usefull for system events
addMessage = function(from, text, time, _class) {
var content, messageElement, nick_re;
if (text === null) {
return null;
}
if (time === null) {
// if the time is null or undefined, use the current time.
time = new Date();
} else if ((time instanceof Date) === false) {
// if it's a timestamp, interpret it
time = new Date(time);
}
//every message you see is actually a table with 3 cols:
// the time,
// the person who caused the event,
// and the content
messageElement = $(document.createElement("table"));
messageElement.addClass("message");
if (_class) {
messageElement.addClass(_class);
}
// sanitize
text = util.toStaticHTML(text);
// If the current user said this, add a special css class
nick_re = new RegExp(CONFIG.nick);
if (nick_re.exec(text)) {
messageElement.addClass("personal");
}
// replace URLs with links
text = text.replace(util.urlRE, '<a target="_blank" href="$&">$&</a>');
content = ("<tr>\n <td class=\"date\">" + (util.timeString(time)) + "</td>\n <td class=\"nick\">" + (util.toStaticHTML(from)) + "</td>\n <td class=\"msg-text\">" + text + "</td>\n</tr>");
messageElement.html(content);
//the log is the stream that we view
$("#log").append(messageElement);
//always view the most recent message when it is added
return scrollDown();
};
updateRSS = function() {
var bytes, megabytes;
bytes = parseInt(rss);
if (bytes) {
megabytes = bytes / (1024 * 1024);
megabytes = Math.round(megabytes * 10) / 10;
return $("#rss").text(megabytes.toString());
}
};
updateUptime = function() {
if (starttime) {
return $("#uptime").text(starttime.toRelativeTime());
}
};
transmission_errors = 0;
first_poll = true;
//process updates if we have any, request updates from the server,
// and call again with response. the last part is like recursion except the call
// is being made from the response handler, and not at some point during the
// function's execution.
longPoll = function(data) {
var _a, _b, _c, _d, message, rss;
if (transmission_errors > 2) {
showConnect();
return null;
}
if (data && data.rss) {
rss = data.rss;
updateRSS();
}
//process any updates we may have
//data will be null on the first call of longPoll
if (data && data.messages) {
_b = data.messages;
for (_a = 0, _c = _b.length; _a < _c; _a++) {
message = _b[_a];
//track oldest message so we only request newer messages from server
message.timestamp > CONFIG.last_message_time ? (CONFIG.last_message_time = message.timestamp) : null;
//dispatch new messages to their appropriate handlers
if ((_d = message.type) === "msg") {
!CONFIG.focus ? CONFIG.unread++ : null;
addMessage(message.nick, message.text, message.timestamp);
} else if (_d === "join") {
userJoin(message.nick, message.timestamp);
} else if (_d === "part") {
userPart(message.nick, message.timestamp);
}
}
//update the document title to include unread message count if blurred
updateTitle();
//only after the first request for messages do we want to show who is here
if (first_poll) {
first_poll = false;
who();
}
}
//make another request
return $.ajax({
cache: false,
type: "GET",
url: "/recv",
dataType: "json",
data: {
since: CONFIG.last_message_time,
id: CONFIG.id
},
error: function() {
addMessage("", "long poll error. trying again...", new Date(), "error");
transmission_errors += 1;
//don't flood the servers on error, wait 10 seconds before retrying
return setTimeout(longPoll, 10 * 1000);
},
success: function(data) {
transmission_errors = 0;
//if everything went well, begin another request immediately
//the server will take a long time to respond
//how long? well, it will wait until there is another message
//and then it will return it to us and close the connection.
//since the connection is closed when we get data, we longPoll again
return longPoll(data);
}
});
};
//submit a new message to the server
send = function(msg) {
if (CONFIG.debug === false) {
// XXX should be POST
// XXX should add to messages immediately
return jQuery.get("/send", {
id: CONFIG.id,
text: msg
}, function(data) {
return true;
}, "json");
}
};
//Transition the page to the state that prompts the user for a nickname
showConnect = function() {
$("#connect").show();
$("#loading").hide();
$("#toolbar").hide();
return $("#nickInput").focus();
};
//transition the page to the loading screen
showLoad = function() {
$("#connect").hide();
$("#loading").show();
return $("#toolbar").hide();
};
//transition the page to the main chat view, putting the cursor in the textfield
showChat = function(nick) {
$("#toolbar").show();
$("#entry").focus();
$("#connect").hide();
$("#loading").hide();
return scrollDown();
};
//we want to show a count of unread messages when the window does not have focus
updateTitle = function() {
if (CONFIG.unread) {
document.title = "(" + CONFIG.unread.toString() + ") node chat";
return document.title;
} else {
document.title = "node chat";
return document.title;
}
};
// daemon start time
starttime = null;
// daemon memory usage
rss = null;
//handle the server's response to our nickname and join request
onConnect = function(session) {
if (session.error) {
alert("error connecting: " + session.error);
showConnect();
return null;
}
CONFIG.nick = session.nick;
CONFIG.id = session.id;
starttime = new Date(session.starttime);
rss = session.rss;
updateRSS();
updateUptime();
//update the UI to show the chat
showChat(CONFIG.nick);
//listen for browser events so we know to update the document title
$(window).bind("blur", function() {
CONFIG.focus = false;
return updateTitle();
});
return $(window).bind("focus", function() {
CONFIG.focus = true;
CONFIG.unread = 0;
return updateTitle();
});
};
//add a list of present chat members to the stream
outputUsers = function() {
var nick_string;
nick_string = nicks.length > 0 ? nicks.join(", ") : "(none)";
addMessage("users:", nick_string, new Date(), "notice");
return false;
};
//get a list of the users presently in the room, and add it to the stream
who = function() {
return jQuery.get("/who", {}, function(data, status) {
if (status !== "success") {
return null;
}
nicks = data.nicks;
return outputUsers();
}, "json");
};
$(document).ready(function() {
//submit new messages when the user hits enter if the message isnt blank
$("#entry").keypress(function(e) {
var msg;
if (e.keyCode !== 13) {
return null;
}
msg = $("#entry").attr("value").replace("\n", "");
!util.isBlank(msg) ? send(msg) : null;
return $("#entry").attr("value", "");
});
$("#usersLink").click(outputUsers);
//try joining the chat when the user clicks the connect button
$("#connectButton").click(function() {
var nick;
//lock the UI while waiting for a response
showLoad();
nick = $("#nickInput").attr("value");
//dont bother the backend if we fail easy validations
if (nick.length > 50) {
alert("Nick too long. 50 character max.");
showConnect();
return false;
}
//more validations
if (/[^\w_\-^!]/.exec(nick)) {
alert("Bad character in nick. Can only have letters, numbers, and '_', '-', '^', '!'");
showConnect();
return false;
}
//make the actual join request to the server
$.ajax({
cache: false,
type: "GET",
// XXX should be POST
dataType: "json",
url: "/join",
data: {
nick: nick
},
error: function() {
alert("error connecting to server");
return showConnect();
},
success: onConnect
});
return false;
});
// update the daemon uptime every 10 seconds
setInterval(function() {
return updateUptime();
}, 10 * 1000);
if (CONFIG.debug) {
$("#loading").hide();
$("#connect").hide();
scrollDown();
return null;
}
// remove fixtures
$("#log table").remove();
//begin listening for updates right away
//interestingly, we don't need to join a room to get its updates
//we just don't show the chat stream to the user until we create a session
longPoll();
return showConnect();
});
//if we can, notify the server that we're going away.
$(window).unload(function() {
return jQuery.get("/part", {
id: CONFIG.id
}, function(data) {
return null;
}, "json");
});