Mercurial > eldonilo > lightstring
comparison lightstring.js @ 33:88d24231bf24
Better newId function.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 28 Jan 2012 04:28:25 +0100 |
parents | 630b9579fe4a |
children | 6ce66fba0242 |
comparison
equal
deleted
inserted
replaced
32:8b739d4e094b | 33:88d24231bf24 |
---|---|
66 * @param {Object} aString DOM object. | 66 * @param {Object} aString DOM object. |
67 * @return {String} Stringified DOM. | 67 * @return {String} Stringified DOM. |
68 */ | 68 */ |
69 dom2xml: function(aElement) { | 69 dom2xml: function(aElement) { |
70 return this.serializer.serializeToString(aElement); | 70 return this.serializer.serializeToString(aElement); |
71 } | 71 }, |
72 /** | |
73 * @function Get an unique identifier. | |
74 * @param {String} [aString] Prefix to put before the identifier. | |
75 * @return {String} Identifier. | |
76 */ | |
77 newId: (function() { | |
78 var id = 1024; | |
79 return function(prefix) { | |
80 if (typeof prefix === 'string') | |
81 return prefix + id++; | |
82 return '' + id++; | |
83 }; | |
84 })(); | |
72 }; | 85 }; |
73 | 86 |
74 /** | 87 /** |
75 * @constructor Creates a new Lightstring connection | 88 * @constructor Creates a new Lightstring connection |
76 * @param {String} [aService] The Websocket service URL. | 89 * @param {String} [aService] The Websocket service URL. |
78 */ | 91 */ |
79 Lightstring.Connection = function(aService) { | 92 Lightstring.Connection = function(aService) { |
80 if (aService) | 93 if (aService) |
81 this.service = aService; | 94 this.service = aService; |
82 this.handlers = {}; | 95 this.handlers = {}; |
83 this.iqid = 1024; | |
84 this.getNewId = function() { | |
85 this.iqid++; | |
86 return 'sendiq:' + this.iqid; | |
87 }; | |
88 this.on('stream:features', function(stanza, that) { | 96 this.on('stream:features', function(stanza, that) { |
89 var nodes = stanza.querySelectorAll('mechanism'); | 97 var nodes = stanza.querySelectorAll('mechanism'); |
90 //SASL/Auth features | 98 //SASL/Auth features |
91 if (nodes.length > 0) { | 99 if (nodes.length > 0) { |
92 that.emit('mechanisms', stanza); | 100 that.emit('mechanisms', stanza); |
298 | 306 |
299 | 307 |
300 if (elm.tagName === 'iq') { | 308 if (elm.tagName === 'iq') { |
301 var id = elm.getAttribute('id'); | 309 var id = elm.getAttribute('id'); |
302 if (!id) { | 310 if (!id) { |
303 elm.setAttribute('id', this.getNewId()); | 311 elm.setAttribute('id', Lightstring.newId('sendiq:')); |
304 str = Lightstring.dom2xml(elm); | 312 str = Lightstring.dom2xml(elm); |
305 } | 313 } |
306 if (aCallback) | 314 if (aCallback) |
307 this.on(elm.getAttribute('id'), aCallback); | 315 this.on(elm.getAttribute('id'), aCallback); |
308 } | 316 } |