Mercurial > cosocket
comparison openwebchat.js @ 64:559c48a58254
Added the option to long poll instead of using mime multipart.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 30 Apr 2009 06:42:13 -0700 |
parents | b7b9932823e4 |
children | 84bf6a3ac4ec |
comparison
equal
deleted
inserted
replaced
63:b19641a0d5ad | 64:559c48a58254 |
---|---|
27 }); | 27 }); |
28 }; | 28 }; |
29 }, | 29 }, |
30 | 30 |
31 startMessageListener: function startMessageListener(options) { | 31 startMessageListener: function startMessageListener(options) { |
32 var self = this; | |
32 var req = new XMLHttpRequest(); | 33 var req = new XMLHttpRequest(); |
33 req.multipart = true; | 34 var basePath = 'listen'; |
35 if (options.useMultipart) { | |
36 req.multipart = true; | |
37 basePath += '/multipart'; | |
38 } | |
34 req.open('GET', | 39 req.open('GET', |
35 'listen/multipart?start=' + options.storage.length, | 40 basePath + '?start=' + options.storage.length, |
36 true); | 41 true); |
37 req.overrideMimeType('application/json'); | 42 req.overrideMimeType('application/json'); |
38 req.addEventListener( | 43 req.addEventListener( |
39 "error", | 44 "error", |
40 function() { | 45 function() { |
43 false | 48 false |
44 ); | 49 ); |
45 req.addEventListener( | 50 req.addEventListener( |
46 "load", | 51 "load", |
47 function onload(evt) { | 52 function onload(evt) { |
48 var msg; | 53 var data; |
49 var errorOccurred = false; | 54 var errorOccurred = false; |
50 try { | 55 try { |
51 msg = JSON.parse(req.responseText); | 56 data = JSON.parse(req.responseText); |
52 } catch (e) { | 57 } catch (e) { |
53 options.onError(e); | 58 options.onError(e); |
54 errorOccurred = true; | 59 errorOccurred = true; |
55 } | 60 } |
56 if (!errorOccurred) { | 61 if (!errorOccurred) { |
57 options.storage.append(msg); | 62 function processMessage(msg) { |
58 options.onMessage(msg); | 63 options.storage.append(msg); |
64 options.onMessage(msg); | |
65 } | |
66 if (options.useMultipart) | |
67 processMessage(data); | |
68 else { | |
69 // TODO: Make sure data.messages is an array. | |
70 for (var i = 0; i < data.messages.length; i++) | |
71 processMessage(data.messages[i]); | |
72 // Start another long poll. | |
73 self.startMessageListener(options); | |
74 } | |
59 } | 75 } |
60 }, | 76 }, |
61 false | 77 false |
62 ); | 78 ); |
63 req.send(null); | 79 req.send(null); |
182 } | 198 } |
183 window.setTimeout(showStoredConversation, UI_BREATHE_TIME); | 199 window.setTimeout(showStoredConversation, UI_BREATHE_TIME); |
184 } else | 200 } else |
185 OpenWebChat.startMessageListener( | 201 OpenWebChat.startMessageListener( |
186 {storage: convStorage, | 202 {storage: convStorage, |
203 useMultipart: false, | |
187 onMessage: onMessage, | 204 onMessage: onMessage, |
188 onError: function onError(exception) { | 205 onError: function onError(exception) { |
189 if (window.console) | 206 if (window.console) |
190 window.console.log('The error', exception, 'occurred.'); | 207 window.console.log('The error', exception, 'occurred.'); |
191 var error = $('#templates .error').clone(); | 208 var error = $('#templates .error').clone(); |