Mercurial > eldonilo > blog
view blog.js @ 8:461a24a5a788
Silent errors when testing if we are server- or client-side.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 02 Nov 2011 09:48:28 -0700 |
parents | 7ab6b48122af |
children | 42d3f454d4b4 |
line wrap: on
line source
//'use strict'; const BOSH_SERVICE = 'http://linkmauve.fr/http-bind/'; var conn = null; var jid = 'blog@linkmauve.fr'; // FIXME: Strophe should accept anonymous connections. var password = 'blog'; var service = 'psgxs.linkmauve.fr'; var node = 'blog'; var re = true; var params = (function() { var vars = {}; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split(';'); for(var i = 0; i < hashes.length; i++) { var s = hashes[i].indexOf('='); var key = hashes[i].substring(0, s); var value = hashes[i].substring(s+1); vars[key] = value; } return vars; })(); if (params.jid) service = params.jid; if (params.node) node = params.node; var received = {}; var messages = document.getElementById('messages'); var updateMessage = function(name, id) { var html = function(name, id) { return received[name][id].html; } var date = function(name, id) { return received[name][id].date; } var divs = messages.getElementsByTagNameNS(ns.xhtml, 'div'); var container = null; for (var i in divs) { var div = divs[i] if (typeof div != 'object') continue; if (div.getAttributeNS(ns.e, 'jid') === name) { container = div; break; } } if (!container) { var container = document.createElementNS(ns.xhtml, 'div'); container.setAttributeNS(ns.e, 'jid', name); messages.appendChild(container); } var articles = container.getElementsByTagNameNS(ns.xhtml, 'article'); for (var i in articles) { var article = articles[i]; if (typeof article != 'object') continue; if (article.getAttributeNS(ns.e, 'id') === id) { container.replaceChild(html(name, id), article); return; } } var article = html(name, id); if (!container.firstChild) container.appendChild(article); else { var d = date(name, id); var toInsert; for (var i in articles) { var a = articles[i]; if (typeof a != 'object') continue; var ad = new Date(); ad.set8601(a.getAttributeNS(ns.e, 'date')); if (ad < d) { toInsert = a; break; } } if (toInsert) container.insertBefore(article, toInsert); else container.appendChild(article); } } var convert = function(id, xml) { var ns = xml['@xmlns']; if (ns in parsers) return new parsers[ns](id, xml); return new parsers[''](id, xml); }; var parsePubSubEvent = function(stanza) { var e = {}; e.service = stanza.getAttribute('from'); var pubsub = stanza.getChild('event', ns.pse); if (!pubsub) { pubsub = stanza.getChild('pubsub', ns.ps); if (!pubsub) return; } e.ns = pubsub.getAttribute('xmlns'); var items = pubsub.getChild('items', e.ns); if (!items) return; e.node = items.getAttribute('node'); items = items.getChildren('item', e.ns); if (!items) return; e.name = e.service + '/' + e.node; e.items = {}; for (var i in items) { var item = items[i]; if (!item.getAttribute) continue; var pl = item.getChild(); if (!pl) continue; var id = item.getAttribute('id'); e.items[id] = pl; } return e; } var onMessages = function(stanza) { conn.addHandler(onMessages, null, 'message', null, null, null); stanza = xml2json(stanza); var e = parsePubSubEvent(stanza); if (!received[e.name]) received[e.name] = {}; for (var id in e.items) { received[e.name][id] = convert(id, e.items[id]); updateMessage(e.name, id); } } var onInfo = function(stanza) { console.log('info'); // TODO! /*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0]; var x = query.getElementsByTagNameNS(ns.data, 'x')[0]; var form = forms.parse(x);*/ } var onSubscribed = function(stanza) { var type = stanza.getAttribute('type'); if (type !== 'result') { messages.innerHTML = 'Error, impossible to retrieve messages.'; conn.disconnect(); } } function onConnect(status) { if (status == Strophe.Status.CONNFAIL) { console.log('Failed to connect.'); } else if (status == Strophe.Status.DISCONNECTING) { console.log('Disconnecting.'); } else if (status == Strophe.Status.DISCONNECTED) { console.log('Disconnected.'); if (re) conn.connect(jid, password, onConnect); } else if (status == Strophe.Status.CONNECTED) { conn.addHandler(onMessages, null, 'message', null, null, null); conn.send($pres().tree()); conn.pubsub.subscribe(jid, service, node, undefined, onMessages, onSubscribed); if (params.no === 'server') { conn.pubsub.items(jid, service, node, onMessages); conn.pubsub.info(jid, service, node, onInfo); } } } window.addEventListener('load', function () { conn = new Strophe.Connection(BOSH_SERVICE); conn.connect(jid, password, onConnect); }, false); window.addEventListener('unload', function (e) { re = false; conn.disconnect(); e.preventDefault(); }, false);