Mercurial > eldonilo > blog
diff blog.js @ 13:161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Drop BOSH support and add WebSockets support.
The server-side is untested, may be broken.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 03 Nov 2011 14:23:10 -0700 |
parents | 42d3f454d4b4 |
children |
line wrap: on
line diff
--- a/blog.js +++ b/blog.js @@ -1,6 +1,6 @@ //'use strict'; -const BOSH_SERVICE = 'http://linkmauve.fr/http-bind/'; +const WS_SERVICE = 'ws://plugsbee.com:5280/'; var conn = null; var jid = 'blog@linkmauve.fr'; // FIXME: Strophe should accept anonymous connections. var password = 'blog'; @@ -100,67 +100,22 @@ var updateMessage = function(name, id) { } } -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 convert = function(item) { + if (item.ns in parsers) + return new parsers[item.ns](item); + return new parsers[''](item); }; -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; +var onMessages = function(stanza) { + var name = stanza.service+'/'+stanza.node; + if (!received[name]) + received[name] = {}; - 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; - } + var r = received[name]; - 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); + for (var id in stanza.items) { + r[id] = convert(stanza.items[id]); + updateMessage(name, id); } } @@ -171,17 +126,16 @@ var onInfo = function(stanza) { var form = forms.parse(x);*/ } -var onSubscribed = function(stanza) { - var type = stanza.getAttribute('type'); - if (type !== 'result') { - messages.innerHTML = 'Error, impossible to retrieve messages.'; - re = false; - conn.disconnect(); +function onConnect() { + conn.send('<presence/>'); + // TODO: verify the subscription. + //conn.pubsubSubscribe(service, node); + if (params.no === 'server') { + conn.pubsubItems(service, node, onMessages); + conn.discoInfo(service, node, onInfo); } -} -function onConnect(status) { - if (status == Strophe.Status.CONNFAIL) { + /*if (status == Strophe.Status.CONNFAIL) { console.log('Failed to connect.'); } else if (status == Strophe.Status.DISCONNECTING) { console.log('Disconnecting.'); @@ -189,20 +143,23 @@ function onConnect(status) { 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); + conn = new XMPP(WS_SERVICE); + + // Debug + conn.on('DOMOutput', function(stanza) { + console.log('out:', stanza); + }); + conn.on('DOMInput', function(stanza) { + console.log('in:', stanza); + }); + + conn.on('connected', onConnect); + conn.on('message', onMessages); + conn.connect(jid, password); }, false); window.addEventListener('unload', function (e) {