Mercurial > cosocket
annotate openwebchat.js @ 49:4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 28 Apr 2009 19:00:16 +0000 |
parents | 9c66f1f840f7 |
children | f78b986ecb6e |
rev | line source |
---|---|
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
1 var OpenWebChat = { |
45
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
2 startMessageListener: function startMessageListener(options) { |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
3 var req = new XMLHttpRequest(); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
4 req.multipart = true; |
44
d6467f3845ad
Changed 'listen' endpoint to 'listen/multipart'.
Atul Varma <varmaa@toolness.com>
parents:
43
diff
changeset
|
5 req.open('GET', 'listen/multipart', true); |
34 | 6 req.overrideMimeType('application/json'); |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
7 req.addEventListener( |
45
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
8 "error", |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
9 function() { |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
10 options.onError(null); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
11 }, |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
12 false |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
13 ); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
14 req.addEventListener( |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
15 "load", |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
16 function onload(evt) { |
45
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
17 var msg; |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
18 var errorOccurred = false; |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
19 try { |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
20 msg = JSON.parse(req.responseText); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
21 } catch (e) { |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
22 options.onError(e); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
23 errorOccurred = true; |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
24 } |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
25 if (!errorOccurred) |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
26 options.onMessage(msg); |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
27 }, |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
28 false |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
29 ); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
30 req.send(null); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
31 }, |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
32 |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
33 sendMessage: function sendMessage(msg) { |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
34 var req = new XMLHttpRequest(); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
35 req.open('POST', 'send', true); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
36 req.overrideMimeType('application/json'); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
37 req.send(JSON.stringify(msg)); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
38 } |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
39 }; |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
40 |
26 | 41 $(window).ready( |
42 function() { | |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
43 var ENTER_KEYCODE = 13; |
49
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
44 var MAX_ANIMATING_MESSAGES = 3; |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
45 |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
46 var animatingMessages = 0; |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
47 |
34 | 48 $('#outgoing-message').focus(); |
49 | |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
50 $('#outgoing-message').keydown( |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
51 function(evt) { |
39 | 52 var content = $(this).val(); |
53 var author = $('#name').val(); | |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
54 if (evt.keyCode == ENTER_KEYCODE) { |
39 | 55 if (content) { |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
56 $(this).val(''); |
40
11c4fb8bec3c
Added timestamp to messages, though they're not currently displayed in any way.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
57 var msg = {content: content, |
11c4fb8bec3c
Added timestamp to messages, though they're not currently displayed in any way.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
58 time: new Date()}; |
39 | 59 if (author) |
60 msg.author = author; | |
61 OpenWebChat.sendMessage(msg); | |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
62 } |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
63 evt.preventDefault(); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
64 } |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
65 }); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
66 |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
67 OpenWebChat.startMessageListener( |
45
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
68 {onMessage: function onMessage(msg) { |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
69 var block = $('#templates .message').clone(); |
38
5fea1533e8ff
Added dynamic detection of HTML.
Atul Varma <varmaa@toolness.com>
parents:
34
diff
changeset
|
70 |
48
9c66f1f840f7
Message content is now just set to HTML; the browser seems to be good at figuring out how to format non-compliant HTML on its own.
Atul Varma <varmaa@toolness.com>
parents:
47
diff
changeset
|
71 $('.content', block).html(msg.content); |
39 | 72 |
45
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
73 var author = msg.author ? msg.author : 'Anonymous'; |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
74 if (author != $('#content .author:last').text()) |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
75 $('.author', block).text(author); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
76 else |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
77 $('.author', block).remove(); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
78 |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
79 $('.timestamp', block).text(msg.time); |
42
f560e6f01c22
misc changes, including now scrolling to the bottom when new messages arrive.
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
80 |
45
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
81 block.hide(); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
82 $('#incoming-messages').append(block); |
49
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
83 |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
84 if (animatingMessages < MAX_ANIMATING_MESSAGES) { |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
85 animatingMessages += 1; |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
86 |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
87 block.slideDown( |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
88 function() { |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
89 animatingMessages -= 1; |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
90 window.scrollTo(0, $('#bottom').position().top); |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
91 }); |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
92 } else |
4e21d301ea27
Added a cap on the max num of messages that can be animating at once, so that browsers don't get overloaded.
Atul Varma <varmaa@toolness.com>
parents:
48
diff
changeset
|
93 block.show(); |
45
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
94 }, |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
95 onError: function onError(exception) { |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
96 if (window.console) |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
97 window.console.log('The error', exception, 'occurred.'); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
98 var error = $('#templates .error').clone(); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
99 error.hide(); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
100 $('#incoming-messages').append(error); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
101 error.slideDown(); |
27785e0ac4d8
Added error detection on the client-side.
Atul Varma <varmaa@toolness.com>
parents:
44
diff
changeset
|
102 } |
33
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
103 }); |
8146a59c8045
Added basic 'chat' functionality.
Atul Varma <varmaa@toolness.com>
parents:
26
diff
changeset
|
104 }); |