comparison 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
comparison
equal deleted inserted replaced
7:853dcbe8f06f 8:7b2ca4d5af6d
109 conn.send(initiate); 109 conn.send(initiate);
110 }; 110 };
111 111
112 var accept = function(sid) { 112 var accept = function(sid) {
113 var doc = documents[sid]; 113 var doc = documents[sid];
114 doc.empty();
115 var accept = "<iq to='" + doc.initiator + "' type='set'>" + 114 var accept = "<iq to='" + doc.initiator + "' type='set'>" +
116 "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" + 115 "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" +
117 " action='session-accept'" + 116 " action='session-accept'" +
118 " initiator='" + doc.initiator + "'" + 117 " initiator='" + doc.initiator + "'" +
119 " sid='" + sid + "'>" + 118 " sid='" + sid + "'>" +
226 delete documents[sid]; 225 delete documents[sid];
227 } 226 }
228 }); 227 });
229 228
230 var connect = function(sid) { 229 var connect = function(sid) {
231 var host = documents[sid].host; 230 var doc = documents[sid];
231 var host = doc.host;
232 var identities = roster[host].identities; 232 var identities = roster[host].identities;
233 var type = 'chat'; 233 var type = 'chat';
234 identities.forEach(function(identity) { 234 identities.forEach(function(identity) {
235 if (identity.category === 'conference') 235 if (identity.category === 'conference')
236 type = 'groupchat'; 236 type = 'groupchat';
242 " session='" + sid + "'>" + 242 " session='" + sid + "'>" +
243 "<connect/>" + 243 "<connect/>" +
244 "</sxe>" + 244 "</sxe>" +
245 "</message>"; 245 "</message>";
246 conn.send(message); 246 conn.send(message);
247 }; 247 doc.participants[conn.jid.full] = type;
248 248 };
249 conn.on('message/' + Lightstring.ns['sxe'] + ':sxe', function(stanza) { 249
250 var from = stanza.DOM.getAttributeNS(null, 'from'); 250 var offerState = function(to, type, sid) {
251 var type = 'chat'; //TODO: always? 251 var message = "<message to='" + to + "'" +
252 var sxe = stanza.DOM.firstChild; //TODO: there can be multiple payloads. 252 " type='" + type + "'>" +
253 var id = sxe.getAttributeNS(null, 'id'); 253 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
254 var sid = sxe.getAttributeNS(null, 'session'); 254 " id='" + Lightstring.newId('sxe') + "'" +
255 255 " session='" + sid + "'>" +
256 "<state-offer>" +
257 "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" +
258 "</state-offer>" +
259 "</sxe>" +
260 "</message>";
261 conn.send(message);
262 };
263
264 var sendState = function(sid) {
256 var doc = documents[sid]; 265 var doc = documents[sid];
257 if (!doc) { 266 var initialState = doc.createState([]).map(function(element) {
258 conn.send("<message to='" + from + "' type='error'/>"); //XXX 267 return Lightstring.DOM2XML(element);
259 return; 268 }).join('');
260 } 269 if (conn.jid.full in doc.participants) { // participant
261 270 var type = 'chat'; //XXX
262 var payload = sxe.firstChild; //TODO: really? 271 var message = "<message to='" + doc.host + "'" +
263 switch (payload.localName) { 272 " type='" + type + "'>" +
264 case 'connect': 273 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
265 var message = "<message to='" + from + "'" + 274 " id='" + Lightstring.newId('sxe') + "'" +
266 " type='" + type + "'>" + 275 " session='" + sid + "'>" +
267 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + 276 "<state>" +
268 " id='" + Lightstring.newId('sxe') + "'" + 277 "<document-begin prolog='" + doc.prolog + "'/>" +
269 " session='" + sid + "'>" + 278 initialState +
270 "<state-offer>" + 279 "<document-end last-sender='' last-id=''/>" +
271 "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" + 280 "</state>" +
272 "</state-offer>" + 281 "</sxe>" +
273 "</sxe>" + 282 "</message>";
274 "</message>"; 283 conn.send(message);
275 conn.send(message); 284 } else { // host
276 break; 285 for (var jid in doc.participants) {
277 case 'state-offer': 286 var type = doc.participants[jid];
278 var description = payload.firstChild; 287 var message = "<message to='" + jid + "'" +
279 if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml'])
280 return terminate(sid, 'unsupported-applications');
281
282 var accept = false;
283 if (from === doc.host || from === doc.initiator)
284 accept = true;
285
286 //TODO: refuse if proposed multiple times.
287
288 var message = "<message to='" + from + "'" +
289 " type='" + type + "'>" +
290 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
291 " id='" + Lightstring.newId('sxe') + "'" +
292 " session='" + sid + "'>" +
293 "<" + (accept? 'accept': 'refuse') + "-state/>" +
294 "</sxe>" +
295 "</message>";
296 conn.send(message);
297 break;
298 case 'accept-state':
299 console.log(doc);
300 var initialState = doc.createState([]);
301 console.log(initialState);
302 initialState = initialState.map(function(element) {
303 return Lightstring.DOM2XML(element);
304 }).join('');
305 console.log(initialState);
306 var message = "<message to='" + from + "'" +
307 " type='" + type + "'>" + 288 " type='" + type + "'>" +
308 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + 289 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
309 " id='" + Lightstring.newId('sxe') + "'" + 290 " id='" + Lightstring.newId('sxe') + "'" +
310 " session='" + sid + "'>" + 291 " session='" + sid + "'>" +
311 "<state>" + 292 "<state>" +
314 "<document-end last-sender='' last-id=''/>" + 295 "<document-end last-sender='' last-id=''/>" +
315 "</state>" + 296 "</state>" +
316 "</sxe>" + 297 "</sxe>" +
317 "</message>"; 298 "</message>";
318 conn.send(message); 299 conn.send(message);
300 }
301 }
302 };
303
304 conn.on('message/' + Lightstring.ns['sxe'] + ':sxe', function(stanza) {
305 var from = stanza.DOM.getAttributeNS(null, 'from');
306 var type = 'chat'; //TODO: always?
307 var sxe = stanza.DOM.firstChild; //TODO: there can be multiple payloads.
308 var id = sxe.getAttributeNS(null, 'id');
309 var sid = sxe.getAttributeNS(null, 'session');
310
311 var doc = documents[sid];
312 if (!doc) {
313 conn.send("<message to='" + from + "' type='error'/>"); //XXX
314 return;
315 }
316
317 var payload = sxe.firstChild; //TODO: really?
318 switch (payload.localName) {
319 case 'connect':
320 offerState(from, type, sid);
321 doc.participants[from] = type;
322 break;
323 case 'state-offer':
324 var description = payload.firstChild;
325 if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml'])
326 return terminate(sid, 'unsupported-applications');
327
328 var accept = /*false;
329 if (from === doc.host || from === doc.initiator)
330 accept =*/ true;
331
332 //TODO: refuse if proposed multiple times.
333
334 var message = "<message to='" + from + "'" +
335 " type='" + type + "'>" +
336 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" +
337 " id='" + Lightstring.newId('sxe') + "'" +
338 " session='" + sid + "'>" +
339 "<" + (accept? 'accept': 'refuse') + "-state/>" +
340 "</sxe>" +
341 "</message>";
342 conn.send(message);
343 break;
344 case 'accept-state':
345 sendState(sid);
319 break; 346 break;
320 case 'refuse-state': 347 case 'refuse-state':
321 terminate(sid, 'decline'); 348 terminate(sid, 'decline');
322 break; 349 break;
323 case 'state': 350 case 'state':
324 var elements = payload.children; 351 var elements = payload.children;
352 doc.empty();
325 doc.processState(from, elements); 353 doc.processState(from, elements);
326 break; 354 break;
327 default: 355 default:
328 var elements = sxe.children; 356 var elements = sxe.children;
329 doc.processState(from, elements); 357 doc.processState(from, elements);
347 375
348 document.getElementById('host').addEventListener('click', function() { 376 document.getElementById('host').addEventListener('click', function() {
349 host(Lightstring.newId('sess'), 'My First XHTML Document'); 377 host(Lightstring.newId('sess'), 'My First XHTML Document');
350 }, false); 378 }, false);
351 379
380 document.getElementById('resend-state').addEventListener('click', function() {
381 for (var sid in documents)
382 sendState(sid);
383 }, false);
384
352 conn.on('output', function(stanza) { 385 conn.on('output', function(stanza) {
353 console.log('out:', stanza.DOM || stanza.XML); 386 console.log('out:', stanza.DOM || stanza.XML);
354 }); 387 });
355 conn.on('input', function(stanza) { 388 conn.on('input', function(stanza) {
356 console.log('in:', stanza.DOM || stanza.XML); 389 console.log('in:', stanza.DOM || stanza.XML);