Mercurial > cosocket
changeset 62:b7b9932823e4
We now store conversations in local/globalStorage.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 29 Apr 2009 06:15:32 -0700 |
parents | c1b46b60e838 |
children | b19641a0d5ad |
files | openwebchat.js |
diffstat | 1 files changed, 95 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/openwebchat.js Tue Apr 28 23:07:12 2009 -0700 +++ b/openwebchat.js Wed Apr 29 06:15:32 2009 -0700 @@ -1,8 +1,39 @@ var OpenWebChat = { + ClientStorage: function ClientStorage(localStorage, prefix) { + var self = this; + if (!localStorage[prefix + 'length']) + localStorage[prefix + 'length'] = '0'; + + self.length = Number(localStorage[prefix + 'length'].value); + + self.get = function get(id) { + return JSON.parse(localStorage[prefix + id].value); + }; + + self.append = function append(msg) { + localStorage[prefix + self.length] = JSON.stringify(msg); + self.length += 1; + localStorage[prefix + 'length'] = self.length.toString(); + }; + + self.wipe = function wipe() { + var names = []; + for (name in localStorage) + names.push(name); + names.forEach( + function(name) { + if (name.indexOf(prefix) == 0) + delete localStorage[name]; + }); + }; + }, + startMessageListener: function startMessageListener(options) { var req = new XMLHttpRequest(); req.multipart = true; - req.open('GET', 'listen/multipart?start=0', true); + req.open('GET', + 'listen/multipart?start=' + options.storage.length, + true); req.overrideMimeType('application/json'); req.addEventListener( "error", @@ -22,8 +53,10 @@ options.onError(e); errorOccurred = true; } - if (!errorOccurred) + if (!errorOccurred) { + options.storage.append(msg); options.onMessage(msg); + } }, false ); @@ -53,6 +86,12 @@ var animatingMessages = 0; var localStorage = globalStorage[document.location.hostname]; + + var convStorage = new OpenWebChat.ClientStorage( + localStorage, + '/conv' + document.location.pathname + ); + if (!localStorage.name) localStorage.name = "A Mysterious Stranger"; @@ -98,44 +137,64 @@ } }); - OpenWebChat.startMessageListener( - {onMessage: function onMessage(msg) { - var block = $('#templates .message').clone(); + function onMessage(msg) { + var block = $('#templates .message').clone(); + + if (localStorage.lastMessage.value == msg.content) + localStorage.lastMessage = ""; - if (localStorage.lastMessage.value == msg.content) - localStorage.lastMessage = ""; + $('.content', block).html(msg.content); - $('.content', block).html(msg.content); + var author = msg.author ? msg.author : 'Anonymous'; + if (author != $('#content .author:last').text()) + $('.author', block).text(author); + else + $('.author', block).remove(); - var author = msg.author ? msg.author : 'Anonymous'; - if (author != $('#content .author:last').text()) - $('.author', block).text(author); - else - $('.author', block).remove(); + $('.timestamp', block).text(msg.time); + + block.hide(); + $('#incoming-messages').append(block); + + if (animatingMessages < MAX_ANIMATING_MESSAGES) { + animatingMessages += 1; - $('.timestamp', block).text(msg.time); - - block.hide(); - $('#incoming-messages').append(block); + block.slideDown( + function() { + animatingMessages -= 1; + window.scrollTo(0, $('#bottom').position().top); + }); + } else + block.show(); + } - if (animatingMessages < MAX_ANIMATING_MESSAGES) { - animatingMessages += 1; + var currStoredMessage = 0; + var CHUNK_SIZE = 20; + var UI_BREATHE_TIME = 10; - block.slideDown( - function() { - animatingMessages -= 1; - window.scrollTo(0, $('#bottom').position().top); - }); - } else - block.show(); - }, - onError: function onError(exception) { - if (window.console) - window.console.log('The error', exception, 'occurred.'); - var error = $('#templates .error').clone(); - error.hide(); - $('#incoming-messages').append(error); - error.slideDown(); - } - }); + function showStoredConversation() { + if (currStoredMessage < convStorage.length) { + var i = 0; + while (i < CHUNK_SIZE && currStoredMessage < convStorage.length) { + onMessage(convStorage.get(currStoredMessage)); + currStoredMessage += 1; + i += 1; + } + window.setTimeout(showStoredConversation, UI_BREATHE_TIME); + } else + OpenWebChat.startMessageListener( + {storage: convStorage, + onMessage: onMessage, + onError: function onError(exception) { + if (window.console) + window.console.log('The error', exception, 'occurred.'); + var error = $('#templates .error').clone(); + error.hide(); + $('#incoming-messages').append(error); + error.slideDown(); + } + }); + } + + showStoredConversation(); });