Mercurial > eldonilo > lightstring
comparison lightstring.js @ 59:bbcc6bbdac73
Make it works :-)
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Wed, 01 Feb 2012 17:47:36 +0100 |
parents | 91f18fdc0e2c |
children | 0e86fca6a596 |
comparison
equal
deleted
inserted
replaced
58:bcb5b7c2c3d3 | 59:bbcc6bbdac73 |
---|---|
111 */ | 111 */ |
112 Lightstring.Connection = function(aService) { | 112 Lightstring.Connection = function(aService) { |
113 if (aService) | 113 if (aService) |
114 this.service = aService; | 114 this.service = aService; |
115 this.handlers = {}; | 115 this.handlers = {}; |
116 this.callbacks = {}; | |
116 this.on('stream:features', function(stanza) { | 117 this.on('stream:features', function(stanza) { |
117 var nodes = stanza.DOM.querySelectorAll('mechanism'); | 118 var nodes = stanza.DOM.querySelectorAll('mechanism'); |
118 //SASL/Auth features | 119 //SASL/Auth features |
119 if (nodes.length > 0) { | 120 if (nodes.length > 0) { |
120 this.emit('mechanisms', stanza); | 121 this.emit('mechanisms', stanza); |
144 } | 145 } |
145 } | 146 } |
146 //XMPP features | 147 //XMPP features |
147 else { | 148 else { |
148 this.emit('features', stanza); | 149 this.emit('features', stanza); |
150 var that = this; | |
149 //Bind http://xmpp.org/rfcs/rfc3920.html#bind | 151 //Bind http://xmpp.org/rfcs/rfc3920.html#bind |
150 var bind = | 152 var bind = |
151 "<iq type='set' xmlns='jabber:client'>" + | 153 "<iq type='set' id='"+Lightstring.newId('sendiq:')+"' xmlns='jabber:client'>" + |
152 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" + | 154 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" + |
153 (this.jid.resource? "<resource>" + this.jid.resource + "</resource>": "") + | 155 (this.jid.resource? "<resource>" + this.jid.resource + "</resource>": "") + |
154 "</bind>" + | 156 "</bind>" + |
155 "</iq>"; | 157 "</iq>"; |
156 | |
157 this.send( | 158 this.send( |
158 bind, | 159 bind, |
160 //Success | |
159 function(stanza) { | 161 function(stanza) { |
160 //Session http://xmpp.org/rfcs/rfc3921.html#session | 162 //Session http://xmpp.org/rfcs/rfc3921.html#session |
163 alert('iq callback!!!'); | |
161 this.jid = new Lightstring.JID(stanza.DOM.textContent); | 164 this.jid = new Lightstring.JID(stanza.DOM.textContent); |
162 this.send( | 165 that.send( |
163 "<iq type='set' xmlns='jabber:client'>" + | 166 "<iq type='set' id='"+Lightstring.newId('sendiq:')+"' xmlns='jabber:client'>" + |
164 "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" + | 167 "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" + |
165 "</iq>", | 168 "</iq>", |
166 function() { | 169 function() { |
167 this.emit('connected'); | 170 that.emit('connected'); |
168 } | 171 } |
169 ); | 172 ); |
173 }, | |
174 //Error | |
175 function(stanza) { | |
176 //TODO: Error? | |
170 } | 177 } |
171 ); | 178 ); |
172 } | 179 } |
173 }); | 180 }); |
174 this.on('success', function(stanza) { | 181 this.on('success', function(stanza) { |
315 var id = stanza.DOM.getAttributeNS(null, 'id'); | 322 var id = stanza.DOM.getAttributeNS(null, 'id'); |
316 if (!(id && id in that.callbacks)) | 323 if (!(id && id in that.callbacks)) |
317 return; | 324 return; |
318 | 325 |
319 var type = stanza.DOM.getAttributeNS(null, 'type'); | 326 var type = stanza.DOM.getAttributeNS(null, 'type'); |
320 if (type !== 'result' || type !== 'error') | 327 if (type !== 'result' && type !== 'error') |
321 return; //TODO: emit an warning, perhaps. | 328 return; //TODO: emit an warning, perhaps. |
322 | 329 |
323 var callback = that.callbacks[id]; | 330 var callback = that.callbacks[id]; |
324 if (type === 'result' && callback.result) | 331 if (type === 'result' && callback.success) |
325 callback.result(stanza); | 332 callback.success(stanza); |
326 else if (type === 'error' && callback.error) | 333 else if (type === 'error' && callback.error) |
327 callback.error(stanza); | 334 callback.error(stanza); |
328 | 335 |
329 delete that.callbacks[id]; | 336 delete that.callbacks[id]; |
330 | 337 |
339 /** | 346 /** |
340 * @function Send a message. | 347 * @function Send a message. |
341 * @param {String|Object} aStanza The message to send. | 348 * @param {String|Object} aStanza The message to send. |
342 * @param {Function} [aCallback] Executed on answer. (stanza must be iq) | 349 * @param {Function} [aCallback] Executed on answer. (stanza must be iq) |
343 */ | 350 */ |
344 send: function(aStanza, aResult, aError) { | 351 send: function(aStanza, aSuccess, aError) { |
345 if (!(aStanza instanceof Lightstring.Stanza)) | 352 if (!(aStanza instanceof Lightstring.Stanza)) |
346 var stanza = new Lightstring.Stanza(aStanza); | 353 var stanza = new Lightstring.Stanza(aStanza); |
347 else | 354 else |
348 var stanza = aStanza; | 355 var stanza = aStanza; |
349 | 356 |
353 if (stanza.DOM.tagName === 'iq') { | 360 if (stanza.DOM.tagName === 'iq') { |
354 var type = stanza.DOM.getAttributeNS(null, 'type'); | 361 var type = stanza.DOM.getAttributeNS(null, 'type'); |
355 if (type !== 'get' || type !== 'set') | 362 if (type !== 'get' || type !== 'set') |
356 ; //TODO: emit an error. | 363 ; //TODO: emit an error. |
357 | 364 |
358 var callback = {result: aResult, error: aError}; | 365 var callback = {success: aSuccess, error: aError}; |
359 | 366 |
360 var id = stanza.DOM.getAttributeNS(null, 'id'); | 367 var id = stanza.DOM.getAttributeNS(null, 'id'); |
361 if (!id) | 368 if (!id) |
362 ; //TODO: emit an warning. | 369 ; //TODO: emit an warning. |
363 else | 370 else |
364 this.callback[id] = callback; | 371 this.callbacks[id] = callback; |
365 | 372 |
366 } else if (aResult || aError) | 373 } else if (aSuccess || aError) |
367 ; //TODO: callback can’t be called with non-iq stanza. | 374 ; //TODO: callback can’t be called with non-iq stanza. |
368 | 375 |
369 | 376 |
370 //TODO this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) | 377 //TODO this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) |
371 var fixme = Lightstring.DOM2XML(stanza.DOM); | 378 var fixme = Lightstring.DOM2XML(stanza.DOM); |