-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.coffee
More file actions
386 lines (314 loc) · 10 KB
/
client.coffee
File metadata and controls
386 lines (314 loc) · 10 KB
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
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::toRelativeTime: (now_threshold) ->
delta: new Date() - this
now_threshold: parseInt(now_threshold, 10)
now_threshold: 0 if isNaN(now_threshold)
return 'Just now' if delta <= now_threshold
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
}
for key, value of conversions
if delta < value
break
else
units: key
delta: delta / value
delta: Math.floor(delta)
units += 's' if delta isnt 1
return [delta, units].join(" ")
Date.fromString: (str) -> new Date(Date.parse(str))
#updates the users link to reflect the number of active users
updateUsersLink: ->
t: nicks.length.toString() + " user"
t += "s" if nicks.length isnt 1
$("#usersLink").text t
#handles another person joining chat
userJoin: (nick, timestamp) ->
#put it in the stream
addMessage nick, "joined", timestamp, "join"
if nicks.indexOf(nick) is -1
#otherwise, add the user to the list
nicks.push nick
#update the UI
updateUsersLink()
#handles someone leaving
userPart: (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
updateUsersLink()
# utility functions
class Util
urlRE: /https?:\/\/([-\w\.]+)+(:\d+)?(\/([^\s]*(\?\S+)?)?)?/g
toStaticHTML: (inputHtml) ->
inputHtml: inputHtml.toString()
inputHtml.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
zeroPad: (digits, n) ->
n = n.toString()
while n.length < digits
n = '0' + n
n
timeString: (date) ->
minutes: date.getMinutes().toString()
hours: date.getHours().toString()
@zeroPad(2, hours) + ":" + @zeroPad(2, minutes)
isBlank: (text) ->
blank: /^\s*$/
text.match(blank) isnt null
util: new Util()
#used to keep the most recent messages visible
scrollDown: ->
window.scrollBy 0, 100000000000000000
$("#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: (from, text, time, _class) ->
return if text is null
if time is null
# if the time is null or undefined, use the current time.
time: new Date()
else if (time instanceof Date) is 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")
messageElement.addClass(_class) if _class
# sanitize
text: util.toStaticHTML(text)
# If the current user said this, add a special css class
nick_re: new RegExp(CONFIG.nick)
messageElement.addClass("personal") if nick_re.exec(text)
# replace URLs with links
text: text.replace(util.urlRE, '<a target="_blank" href="$&">$&</a>')
content: """
<tr>
<td class="date">${ util.timeString(time) }</td>
<td class="nick">${ util.toStaticHTML(from) }</td>
<td class="msg-text">$text</td>
</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
scrollDown()
updateRSS: ->
bytes: parseInt(rss)
if bytes
megabytes: bytes / (1024*1024)
megabytes: Math.round(megabytes*10)/10
$("#rss").text(megabytes.toString())
updateUptime: ->
$("#uptime").text(starttime.toRelativeTime()) if starttime
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: (data) ->
if transmission_errors > 2
showConnect()
return
if data and data.rss
rss: data.rss
updateRSS()
#process any updates we may have
#data will be null on the first call of longPoll
if data and data.messages
for message in data.messages
#track oldest message so we only request newer messages from server
if message.timestamp > CONFIG.last_message_time
CONFIG.last_message_time: message.timestamp
#dispatch new messages to their appropriate handlers
switch message.type
when "msg"
if !CONFIG.focus
CONFIG.unread++
addMessage message.nick, message.text, message.timestamp
when "join" then userJoin(message.nick, message.timestamp)
when "part" then 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
$.ajax({
cache: false
type: "GET"
url: "/recv"
dataType: "json"
data: { since: CONFIG.last_message_time, id: CONFIG.id }
error: ->
addMessage("", "long poll error. trying again...", new Date(), "error")
transmission_errors += 1
#don't flood the servers on error, wait 10 seconds before retrying
setTimeout(longPoll, 10*1000)
success: (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
longPoll(data)
})
#submit a new message to the server
send: (msg) ->
if CONFIG.debug is false
# XXX should be POST
# XXX should add to messages immediately
jQuery.get("/send", {id: CONFIG.id, text: msg}, (data) ->
return true
"json")
#Transition the page to the state that prompts the user for a nickname
showConnect: ->
$("#connect").show()
$("#loading").hide()
$("#toolbar").hide()
$("#nickInput").focus()
#transition the page to the loading screen
showLoad: ->
$("#connect").hide()
$("#loading").show()
$("#toolbar").hide()
#transition the page to the main chat view, putting the cursor in the textfield
showChat: (nick) ->
$("#toolbar").show()
$("#entry").focus()
$("#connect").hide()
$("#loading").hide()
scrollDown()
#we want to show a count of unread messages when the window does not have focus
updateTitle: ->
if CONFIG.unread
document.title: "(" + CONFIG.unread.toString() + ") node chat"
else
document.title = "node chat"
# daemon start time
starttime: null
# daemon memory usage
rss: null
#handle the server's response to our nickname and join request
onConnect: (session) ->
if session.error
alert("error connecting: " + session.error)
showConnect()
return
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", ->
CONFIG.focus: false
updateTitle()
)
$(window).bind("focus", ->
CONFIG.focus: true
CONFIG.unread: 0
updateTitle()
)
#add a list of present chat members to the stream
outputUsers: ->
nick_string: if nicks.length > 0 then nicks.join(", ") else "(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: ->
jQuery.get("/who", {}, (data, status) ->
if status isnt "success" then return
nicks = data.nicks
outputUsers()
"json"
)
$(document).ready( ->
#submit new messages when the user hits enter if the message isnt blank
$("#entry").keypress( (e) ->
if e.keyCode isnt 13 then return
msg: $("#entry").attr("value").replace("\n", "")
if not util.isBlank(msg) then send(msg)
$("#entry").attr("value", "")
)
$("#usersLink").click(outputUsers)
#try joining the chat when the user clicks the connect button
$("#connectButton").click( ->
#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: ->
alert("error connecting to server")
showConnect()
success: onConnect
})
return false
)
# update the daemon uptime every 10 seconds
setInterval( ->
updateUptime()
10*1000)
if CONFIG.debug
$("#loading").hide()
$("#connect").hide()
scrollDown()
return
# 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()
showConnect()
)
#if we can, notify the server that we're going away.
$(window).unload( ->
jQuery.get("/part", {id: CONFIG.id}, (data) ->
return
"json")
)