diff 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
line wrap: on
line diff
--- a/lightstring.js
+++ b/lightstring.js
@@ -38,6 +38,7 @@ Lightstring.Connection = function (aServ
     return serializer.serializeToString(elm);
   };
   this.connect = function(aJid, aPassword) {
+    this.emit('connecting');
     if(aJid)
       this.jid = aJid;
     if(this.jid) {
@@ -55,24 +56,23 @@ Lightstring.Connection = function (aServ
     if(!this.service)
       throw "Lightstring: Connection.service is undefined.";
 
-    //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635   
-    if(MozWebSocket)
+    //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635
+    try {
+      this.socket = new WebSocket(this.service);
+    }
+    catch(error) {
       this.socket = new MozWebSocket(this.service);
-    else if(WebSocket)
-      this.socket = new WebSocket(this.service);
-    else
-      this.emit('error', 'No WebSocket support.');
+    }
 
     var that = this;
     this.socket.addEventListener('open', function() {
-      that.emit('connecting');
-      //FIXME there shouldn't be an ending "/"
-      that.send(
+      var stream =
         "<stream:stream to='"+that.domain+"'\
                         xmlns='jabber:client'\
                         xmlns:stream='http://etherx.jabber.org/streams'\
-                        version='1.0'/>"
-      );
+                        version='1.0'/>";
+      that.socket.send(stream)
+      that.emit('XMLOutput', stream);
     });
     this.socket.addEventListener('error', function(e) {
       that.emit('error', e.data);
@@ -126,7 +126,6 @@ Lightstring.Connection = function (aServ
 		this.emit('disconnecting');
 		this.send('</stream:stream>');
 		this.socket.close();
-    this.emit('disconnected');
 	};
   this.emit = function(name, data) {
     var handlers = this.handlers[name];
@@ -213,6 +212,10 @@ Lightstring.Connection = function (aServ
     );
   });
   //Internal
+  this.on('failure', function(stanza, that) {
+    that.emit('conn-error', stanza.firstChild.tagName);
+  });
+  //Internal
   this.on('challenge', function(stanza, that) {
     //FIXME this is mostly Strophe code