comparison lightstring.js @ 3:029c12b8f048

various bug fixes and improvements
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 14 Jan 2012 17:36:10 +0100
parents f31a75c3b6c8
children b7a582a2b32c
comparison
equal deleted inserted replaced
2:f31a75c3b6c8 3:029c12b8f048
36 }; 36 };
37 this.serialize = function(elm) { 37 this.serialize = function(elm) {
38 return serializer.serializeToString(elm); 38 return serializer.serializeToString(elm);
39 }; 39 };
40 this.connect = function(aJid, aPassword) { 40 this.connect = function(aJid, aPassword) {
41 this.emit('connecting');
41 if(aJid) 42 if(aJid)
42 this.jid = aJid; 43 this.jid = aJid;
43 if(this.jid) { 44 if(this.jid) {
44 this.domain = this.jid.split('@')[1]; 45 this.domain = this.jid.split('@')[1];
45 this.node = this.jid.split('@')[0]; 46 this.node = this.jid.split('@')[0];
53 if(!this.password) 54 if(!this.password)
54 throw "Lightstring: Connection.password is undefined."; 55 throw "Lightstring: Connection.password is undefined.";
55 if(!this.service) 56 if(!this.service)
56 throw "Lightstring: Connection.service is undefined."; 57 throw "Lightstring: Connection.service is undefined.";
57 58
58 //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635 59 //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635
59 if(MozWebSocket) 60 try {
61 this.socket = new WebSocket(this.service);
62 }
63 catch(error) {
60 this.socket = new MozWebSocket(this.service); 64 this.socket = new MozWebSocket(this.service);
61 else if(WebSocket) 65 }
62 this.socket = new WebSocket(this.service);
63 else
64 this.emit('error', 'No WebSocket support.');
65 66
66 var that = this; 67 var that = this;
67 this.socket.addEventListener('open', function() { 68 this.socket.addEventListener('open', function() {
68 that.emit('connecting'); 69 var stream =
69 //FIXME there shouldn't be an ending "/"
70 that.send(
71 "<stream:stream to='"+that.domain+"'\ 70 "<stream:stream to='"+that.domain+"'\
72 xmlns='jabber:client'\ 71 xmlns='jabber:client'\
73 xmlns:stream='http://etherx.jabber.org/streams'\ 72 xmlns:stream='http://etherx.jabber.org/streams'\
74 version='1.0'/>" 73 version='1.0'/>";
75 ); 74 that.socket.send(stream)
75 that.emit('XMLOutput', stream);
76 }); 76 });
77 this.socket.addEventListener('error', function(e) { 77 this.socket.addEventListener('error', function(e) {
78 that.emit('error', e.data); 78 that.emit('error', e.data);
79 }); 79 });
80 this.socket.addEventListener('close', function(e) { 80 this.socket.addEventListener('close', function(e) {
124 }; 124 };
125 this.disconnect = function() { 125 this.disconnect = function() {
126 this.emit('disconnecting'); 126 this.emit('disconnecting');
127 this.send('</stream:stream>'); 127 this.send('</stream:stream>');
128 this.socket.close(); 128 this.socket.close();
129 this.emit('disconnected');
130 }; 129 };
131 this.emit = function(name, data) { 130 this.emit = function(name, data) {
132 var handlers = this.handlers[name]; 131 var handlers = this.handlers[name];
133 if(!handlers) 132 if(!handlers)
134 return; 133 return;
209 "<stream:stream to='"+that.domain+"'\ 208 "<stream:stream to='"+that.domain+"'\
210 xmlns='jabber:client'\ 209 xmlns='jabber:client'\
211 xmlns:stream='http://etherx.jabber.org/streams'\ 210 xmlns:stream='http://etherx.jabber.org/streams'\
212 version='1.0' />" 211 version='1.0' />"
213 ); 212 );
213 });
214 //Internal
215 this.on('failure', function(stanza, that) {
216 that.emit('conn-error', stanza.firstChild.tagName);
214 }); 217 });
215 //Internal 218 //Internal
216 this.on('challenge', function(stanza, that) { 219 this.on('challenge', function(stanza, that) {
217 //FIXME this is mostly Strophe code 220 //FIXME this is mostly Strophe code
218 221