diff options
Diffstat (limited to 'lib.js')
-rw-r--r-- | lib.js | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -29,6 +29,48 @@ function getKeys () { } } +function requestFeed (src, server) { + var ws = new WebSocket(server + src) + + var clientLog = { + publicKey: src + } + + if (localStorage[src]) { + clientLog.log = JSON.parse(localStorage[src]) + } else { + clientLog.log = [] + } + + ws.onopen = function () { + ws.send(JSON.stringify(clientLog)) + } + + ws.onmessage = function (ev) { + var serverData = JSON.parse(ev.data) + if (serverData.log.length > clientLog.log.length) { + + // update the log of the id + localStorage[src] = JSON.stringify(serverData.log) + + // contact new items from the log onto the client's log of everything + var num = serverData.log.length - clientLog.log.length + var diff = serverData.log.slice(0, num) + + if (localStorage['log']) { + var oldLog = JSON.parse(localStorage['log']) + } else { + var oldLog = [] + } + + var newLog = diff.concat(oldLog) + localStorage['log'] = JSON.stringify(newLog) + + location.reload() + } + } +} + // publish new messages to your log function publish (content, keys) { |