# HG changeset patch # User Emmanuel Gil Peyrot # Date 1328193925 -3600 # Node ID 7b2ca4d5af6df9bffcae5b68e6807f74c8a36986 # Parent 853dcbe8f06f40823eb2c1375ce6ee770663a275 Add a resend-whole-state button and make it works. diff --git a/config.js b/config.js --- a/config.js +++ b/config.js @@ -1,5 +1,5 @@ 'use strict'; -const SERVICE = 'ws://plugsbee.com:5280/'; -const JID = 'test@linkmauve.fr'; -const PASSWORD = 'test'; +var SERVICE = 'ws://ws.plugsbee.com/'; +var JID = 'test@linkmauve.fr'; +var PASSWORD = 'test'; diff --git a/index.xhtml b/index.xhtml --- a/index.xhtml +++ b/index.xhtml @@ -19,6 +19,7 @@
+
edit:
@@ -100,7 +101,7 @@

Go ahead, edit away!

-

Here's a typical paragraph element

+

Here's a typical paragraph element

  1. and now a list
  2. with only
  3. three items
diff --git a/script2.js b/script2.js --- 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 = "" + "" + ""; conn.send(message); + doc.participants[conn.jid.full] = type; +}; + +var offerState = function(to, type, sid) { + var 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 = "" + + "" + + "" + + "" + + initialState + + "" + + "" + + "" + + ""; + conn.send(message); + } else { // host + for (var jid in doc.participants) { + var type = doc.participants[jid]; + var message = "" + + "" + + "" + + "" + + initialState + + "" + + "" + + "" + + ""; + 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 = "" + - "" + - "" + - "" + - "" + - "" + - ""; - 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 = "" + - "" + - "" + - "" + - initialState + - "" + - "" + - "" + - ""; - 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); }); diff --git a/sxe-document.js b/sxe-document.js --- a/sxe-document.js +++ b/sxe-document.js @@ -25,6 +25,7 @@ var Document = function(initiator, name, this.initiator = initiator; this.name = name; this.host = host; + this.participants = {}; this.prolog = prolog || 'data:application/xhtml+xml,%3C%3Fxml%20version%3D%271.0%27%3F%3E%0A%3C%21DOCTYPE%20html%3E%0A'; this.state = 'not-started'; @@ -87,8 +88,8 @@ Document.prototype = { var i = 0; var first = elements[0]; if (first.localName === 'document-begin') { - if (this.state !== 'not-started') - return; //TODO: the session has already started. + /*if (this.state !== 'not-started') + return;*/ //TODO: the session has already started. i = 1; this.prolog = first.getAttributeNS(null, 'prolog'); this.state = 'getting-session'; @@ -123,7 +124,7 @@ Document.prototype = { for (var i = 0; i < children.length; i++) { var child = children[i]; - var element = document.createElementNS(Lightstring.NS.sxe, 'new'); + var element = document.createElementNS(Lightstring.ns['sxe'], 'new'); var rid = Lightstring.newId('GUID'); element.setAttributeNS(null, 'rid', rid); @@ -139,7 +140,7 @@ Document.prototype = { //TODO: move that elsewhere, or make it prettier. var convertAttr = function(attr) { - var element = document.createElementNS(Lightstring.NS.sxe, 'new'); + var element = document.createElementNS(Lightstring.ns['sxe'], 'new'); element.setAttributeNS(null, 'type', 'attr'); var arid = Lightstring.newId('GUID'); element.setAttributeNS(null, 'rid', arid);