Mercurial > eldonilo > barbecue
diff script2.js @ 8:7b2ca4d5af6d
Add a resend-whole-state button and make it works.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 02 Feb 2012 15:45:25 +0100 |
parents | 853dcbe8f06f |
children | d3b37bbb2b88 |
line wrap: on
line diff
--- a/script2.js +++ b/script2.js @@ -111,7 +111,6 @@ var initiate = function(jid, sid) { var accept = function(sid) { var doc = documents[sid]; - doc.empty(); var accept = "<iq to='" + doc.initiator + "' type='set'>" + "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" + " action='session-accept'" + @@ -228,7 +227,8 @@ conn.on('iq/' + Lightstring.ns.jingle + }); var connect = function(sid) { - var host = documents[sid].host; + var doc = documents[sid]; + var host = doc.host; var identities = roster[host].identities; var type = 'chat'; identities.forEach(function(identity) { @@ -244,6 +244,61 @@ var connect = function(sid) { "</sxe>" + "</message>"; conn.send(message); + doc.participants[conn.jid.full] = type; +}; + +var offerState = function(to, type, sid) { + var message = "<message to='" + to + "'" + + " type='" + type + "'>" + + "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + + " id='" + Lightstring.newId('sxe') + "'" + + " session='" + sid + "'>" + + "<state-offer>" + + "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" + + "</state-offer>" + + "</sxe>" + + "</message>"; + conn.send(message); +}; + +var sendState = function(sid) { + var doc = documents[sid]; + var initialState = doc.createState([]).map(function(element) { + return Lightstring.DOM2XML(element); + }).join(''); + if (conn.jid.full in doc.participants) { // participant + var type = 'chat'; //XXX + var message = "<message to='" + doc.host + "'" + + " type='" + type + "'>" + + "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + + " id='" + Lightstring.newId('sxe') + "'" + + " session='" + sid + "'>" + + "<state>" + + "<document-begin prolog='" + doc.prolog + "'/>" + + initialState + + "<document-end last-sender='' last-id=''/>" + + "</state>" + + "</sxe>" + + "</message>"; + conn.send(message); + } else { // host + for (var jid in doc.participants) { + var type = doc.participants[jid]; + var message = "<message to='" + jid + "'" + + " type='" + type + "'>" + + "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + + " id='" + Lightstring.newId('sxe') + "'" + + " session='" + sid + "'>" + + "<state>" + + "<document-begin prolog='" + doc.prolog + "'/>" + + initialState + + "<document-end last-sender='' last-id=''/>" + + "</state>" + + "</sxe>" + + "</message>"; + conn.send(message); + } + } }; conn.on('message/' + Lightstring.ns['sxe'] + ':sxe', function(stanza) { @@ -262,26 +317,17 @@ conn.on('message/' + Lightstring.ns['sxe var payload = sxe.firstChild; //TODO: really? switch (payload.localName) { case 'connect': - var message = "<message to='" + from + "'" + - " type='" + type + "'>" + - "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + - " id='" + Lightstring.newId('sxe') + "'" + - " session='" + sid + "'>" + - "<state-offer>" + - "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" + - "</state-offer>" + - "</sxe>" + - "</message>"; - conn.send(message); + offerState(from, type, sid); + doc.participants[from] = type; break; case 'state-offer': var description = payload.firstChild; if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml']) return terminate(sid, 'unsupported-applications'); - var accept = false; + var accept = /*false; if (from === doc.host || from === doc.initiator) - accept = true; + accept =*/ true; //TODO: refuse if proposed multiple times. @@ -296,32 +342,14 @@ conn.on('message/' + Lightstring.ns['sxe conn.send(message); break; case 'accept-state': - console.log(doc); - var initialState = doc.createState([]); - console.log(initialState); - initialState = initialState.map(function(element) { - return Lightstring.DOM2XML(element); - }).join(''); - console.log(initialState); - var message = "<message to='" + from + "'" + - " type='" + type + "'>" + - "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + - " id='" + Lightstring.newId('sxe') + "'" + - " session='" + sid + "'>" + - "<state>" + - "<document-begin prolog='" + doc.prolog + "'/>" + - initialState + - "<document-end last-sender='' last-id=''/>" + - "</state>" + - "</sxe>" + - "</message>"; - conn.send(message); + sendState(sid); break; case 'refuse-state': terminate(sid, 'decline'); break; case 'state': var elements = payload.children; + doc.empty(); doc.processState(from, elements); break; default: @@ -349,6 +377,11 @@ document.getElementById('host').addEvent host(Lightstring.newId('sess'), 'My First XHTML Document'); }, false); +document.getElementById('resend-state').addEventListener('click', function() { + for (var sid in documents) + sendState(sid); +}, false); + conn.on('output', function(stanza) { console.log('out:', stanza.DOM || stanza.XML); });