changeset 85:d9804e206393

Fix Safari/iOS support.
author Sonny Piers <sonny.piers@gmail.com>
date Wed, 08 Feb 2012 17:44:06 +0100
parents 7f4e43d6cb15
children 0c10c9a69c69
files lightstring.js polyfill.js
diffstat 2 files changed, 35 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lightstring.js
+++ b/lightstring.js
@@ -151,20 +151,26 @@ Lightstring.Connection.prototype = {
     if (!this.service)
       return; //TODO: error
 
-    if (typeof(WebSocket) === "function") {
+    // Standard
+    if (typeof(WebSocket) === 'function')
       this.socket = new WebSocket(this.service, 'xmpp');
-    }
-    else if (typeof(MozWebSocket) === "function") {
+    // Safari
+    else if (typeof(WebSocket) === 'object')
+      this.socket = new WebSocket(this.service);
+    // Old Gecko
+    else if (typeof(MozWebSocket) === 'function') {
       this.socket = new MozWebSocket(this.service, 'xmpp');
     }
+    // No known WebSocket support
     else {
       return; //TODO: error
     }
 
     var Conn = this;
     this.socket.addEventListener('open', function() {
-      if (this.protocol !== 'xmpp')
-        return; //TODO: error
+      //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism.
+      //if (this.protocol !== 'xmpp')
+        //return; //TODO: error
 
       var stream = Lightstring.stanzas.stream.open(Conn.jid.domain);
       //FIXME: Use Lightstring.Connection.send (problem with parsing steam);
new file mode 100644
--- /dev/null
+++ b/polyfill.js
@@ -0,0 +1,24 @@
+//js-core doesn't support bind, polyill taken from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
+if (!Function.prototype.bind) {  
+  Function.prototype.bind = function (oThis) {  
+    if (typeof this !== "function") {  
+      // closest thing possible to the ECMAScript 5 internal IsCallable function  
+      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");  
+    }  
+  
+    var aArgs = Array.prototype.slice.call(arguments, 1),   
+        fToBind = this,   
+        fNOP = function () {},  
+        fBound = function () {  
+          return fToBind.apply(this instanceof fNOP  
+                                 ? this  
+                                 : oThis || window,  
+                               aArgs.concat(Array.prototype.slice.call(arguments)));  
+        };  
+  
+    fNOP.prototype = this.prototype;  
+    fBound.prototype = new fNOP();  
+  
+    return fBound;  
+  };  
+}