Mercurial > cosocket
diff 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 |
line wrap: on
line diff
--- a/openwebchat.js Wed Apr 29 16:26:21 2009 -0700 +++ b/openwebchat.js Thu Apr 30 06:42:13 2009 -0700 @@ -29,10 +29,15 @@ }, startMessageListener: function startMessageListener(options) { + var self = this; var req = new XMLHttpRequest(); - req.multipart = true; + var basePath = 'listen'; + if (options.useMultipart) { + req.multipart = true; + basePath += '/multipart'; + } req.open('GET', - 'listen/multipart?start=' + options.storage.length, + basePath + '?start=' + options.storage.length, true); req.overrideMimeType('application/json'); req.addEventListener( @@ -45,17 +50,28 @@ req.addEventListener( "load", function onload(evt) { - var msg; + var data; var errorOccurred = false; try { - msg = JSON.parse(req.responseText); + data = JSON.parse(req.responseText); } catch (e) { options.onError(e); errorOccurred = true; } if (!errorOccurred) { - options.storage.append(msg); - options.onMessage(msg); + function processMessage(msg) { + options.storage.append(msg); + options.onMessage(msg); + } + if (options.useMultipart) + processMessage(data); + else { + // TODO: Make sure data.messages is an array. + for (var i = 0; i < data.messages.length; i++) + processMessage(data.messages[i]); + // Start another long poll. + self.startMessageListener(options); + } } }, false @@ -184,6 +200,7 @@ } else OpenWebChat.startMessageListener( {storage: convStorage, + useMultipart: false, onMessage: onMessage, onError: function onError(exception) { if (window.console)