comparison lightstring.js @ 97:11e38a9bfe38

multi-transport is now possible
author Sonny Piers <sonny.piers@gmail.com>
date Tue, 12 Jun 2012 16:35:27 +0200
parents b211a00efa7f
children f14558915187
comparison
equal deleted inserted replaced
96:646695bde8e9 97:11e38a9bfe38
148 if (!this.jid.bare) 148 if (!this.jid.bare)
149 return; //TODO: error 149 return; //TODO: error
150 if (!this.service) 150 if (!this.service)
151 return; //TODO: error 151 return; //TODO: error
152 152
153 // Standard 153 function getProtocol(aURL) {
154 if (typeof(WebSocket) === 'function') 154 var a = document.createElement('a');
155 this.socket = new WebSocket(this.service, 'xmpp'); 155 a.href = aURL;
156 // Safari 156 return a.protocol.replace(':', '');
157 else if (typeof(WebSocket) === 'object') 157 }
158 this.socket = new WebSocket(this.service, 'xmpp'); 158 var protocol = getProtocol(this.service);
159 // Old Gecko 159
160 else if (typeof(MozWebSocket) === 'function') { 160 if (protocol.match('http'))
161 this.socket = new MozWebSocket(this.service, 'xmpp'); 161 this.connection = new Lightstring.BOSHConnection(this.service);
162 } 162 else if (protocol.match('ws'))
163 // No known WebSocket support 163 this.connection = new Lightstring.WebSocketConnection(this.service);
164 else { 164
165 return; //TODO: error 165 this.connection.connect();
166 } 166
167 167 var that = this;
168 var Conn = this; 168
169 this.socket.addEventListener('open', function() { 169 this.connection.once('open', function() {
170 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism. 170 var stream = Lightstring.stanzas.stream.open(that.jid.domain);
171 //if (this.protocol !== 'xmpp') 171 that.connection.send(stream);
172 //return; //TODO: error
173
174 var stream = Lightstring.stanzas.stream.open(Conn.jid.domain);
175 this.send(stream);
176 var stanza = { 172 var stanza = {
177 XML: stream 173 XML: stream
178 }; 174 };
179 Conn.emit('output', stanza); 175 that.emit('output', stanza);
180 }); 176 });
181 this.socket.addEventListener('error', function(e) { 177 this.connection.on('stanza', function(stanza) {
182 Conn.emit('disconnecting', e.data); 178 var stanza = new Lightstring.Stanza(stanza);
183 //TODO: error
184 });
185 this.socket.addEventListener('close', function(e) {
186 Conn.emit('disconnected', e.data);
187 });
188 this.socket.addEventListener('message', function(e) {
189 var stanza = new Lightstring.Stanza(e.data);
190 179
191 //FIXME: node-xmpp-bosh sends a self-closing stream:stream tag; it is wrong! 180 //FIXME: node-xmpp-bosh sends a self-closing stream:stream tag; it is wrong!
192 Conn.emit('input', stanza); 181 that.emit('input', stanza);
193 182
194 if (!stanza.DOM) 183 if (!stanza.DOM)
195 return; 184 return;
196 185
197 var name = stanza.DOM.localName; 186 var name = stanza.DOM.localName;
203 if (stanza.DOM.firstChild.localName === 'mechanisms') { 192 if (stanza.DOM.firstChild.localName === 'mechanisms') {
204 stanza.mechanisms = []; 193 stanza.mechanisms = [];
205 var nodes = stanza.DOM.getElementsByTagName('mechanism'); 194 var nodes = stanza.DOM.getElementsByTagName('mechanism');
206 for (var i = 0; i < nodes.length; i++) 195 for (var i = 0; i < nodes.length; i++)
207 stanza.mechanisms.push(nodes[i].textContent); 196 stanza.mechanisms.push(nodes[i].textContent);
208 Conn.emit('mechanisms', stanza); 197 that.emit('mechanisms', stanza);
209 } 198 }
210 //XMPP features 199 //XMPP features
211 else { 200 else {
212 //TODO: stanza.features 201 //TODO: stanza.features
213 Conn.emit('features', stanza); 202 that.emit('features', stanza);
214 } 203 }
215 } 204 }
216 else if (name === 'challenge') { 205 else if (name === 'challenge') {
217 Conn.emit('challenge', stanza); 206 that.emit('challenge', stanza);
218 } 207 }
219 else if (name === 'failure') { 208 else if (name === 'failure') {
220 Conn.emit('failure', stanza); 209 that.emit('failure', stanza);
221 } 210 }
222 else if (name === 'success') { 211 else if (name === 'success') {
223 Conn.emit('success', stanza); 212 that.emit('success', stanza);
224 } 213 }
225 214
226 //Iq callbacks 215 //Iq callbacks
227 else if (name === 'iq') { 216 else if (name === 'iq') {
228 var payload = stanza.DOM.firstChild; 217 var payload = stanza.DOM.firstChild;
229 if (payload) 218 if (payload)
230 Conn.emit('iq/' + payload.namespaceURI + ':' + payload.localName, stanza); 219 that.emit('iq/' + payload.namespaceURI + ':' + payload.localName, stanza);
231 220
232 var id = stanza.DOM.getAttribute('id'); 221 var id = stanza.DOM.getAttribute('id');
233 if (!(id && id in Conn.callbacks)) 222 if (!(id && id in that.callbacks))
234 return; 223 return;
235 224
236 var type = stanza.DOM.getAttribute('type'); 225 var type = stanza.DOM.getAttribute('type');
237 if (type !== 'result' && type !== 'error') 226 if (type !== 'result' && type !== 'error')
238 return; //TODO: warning 227 return; //TODO: warning
239 228
240 var callback = Conn.callbacks[id]; 229 var callback = that.callbacks[id];
241 if (type === 'result' && callback.success) 230 if (type === 'result' && callback.success)
242 callback.success.call(Conn, stanza); 231 callback.success.call(that, stanza);
243 else if (type === 'error' && callback.error) 232 else if (type === 'error' && callback.error)
244 callback.error.call(Conn, stanza); 233 callback.error.call(that, stanza);
245 234
246 delete Conn.callbacks[id]; 235 delete that.callbacks[id];
247 } 236 }
248 237
249 else if (name === 'presence' || name === 'message') { 238 else if (name === 'presence' || name === 'message') {
250 Conn.emit(name, stanza); 239 that.emit(name, stanza);
251 } 240 }
252 }); 241 });
253 }, 242 },
254 /** 243 /**
255 * @function Send a message. 244 * @function Send a message.
286 275
287 276
288 //FIXME this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) 277 //FIXME this.socket.send(stanza.XML); (need some work on Lightstring.Stanza)
289 var fixme = Lightstring.DOM2XML(stanza.DOM); 278 var fixme = Lightstring.DOM2XML(stanza.DOM);
290 stanza.XML = fixme; 279 stanza.XML = fixme;
291 this.socket.send(fixme); 280 this.connection.send(fixme);
292 this.emit('output', stanza); 281 this.emit('output', stanza);
293 }, 282 },
294 /** 283 /**
295 * @function Closes the XMPP stream and the socket. 284 * @function Closes the XMPP stream and the socket.
296 */ 285 */