diff options
author | Loïc Hoguin <[email protected]> | 2013-06-07 19:36:16 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2013-06-07 19:36:16 +0200 |
commit | 14b83979aa35d0ca2241819b26c2d22865dbdcd5 (patch) | |
tree | 1dd88ba38f403b07473ca508868c8175ed8a535f /priv | |
parent | d0191203c04e2b87d4715292bc67c24c80ffa22c (diff) | |
parent | 954b4f29ee7c6edddaa462041d6a8d45cf5ba7bb (diff) | |
download | bullet-14b83979aa35d0ca2241819b26c2d22865dbdcd5.tar.gz bullet-14b83979aa35d0ca2241819b26c2d22865dbdcd5.tar.bz2 bullet-14b83979aa35d0ca2241819b26c2d22865dbdcd5.zip |
Merge branch 'eventsource' of git://github.com/jdavisp3/bullet
Diffstat (limited to 'priv')
-rw-r--r-- | priv/bullet.js | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/priv/bullet.js b/priv/bullet.js index fe4df41..50686f6 100644 --- a/priv/bullet.js +++ b/priv/bullet.js @@ -32,7 +32,7 @@ onheartbeat is called once every few seconds to allow you to easily setup a ping/pong mechanism. */ -(function($){$.extend({bullet: function(url){ +(function($){$.extend({bullet: function(url, options){ var CONNECTING = 0; var OPEN = 1; var CLOSING = 2; @@ -47,6 +47,10 @@ websocket: function(){ var transport = null; + if (options !== undefined && options.disableWebSocket) { + return false; + } + if (window.WebSocket){ transport = window.WebSocket; } @@ -63,7 +67,54 @@ return null; }, + eventsource: function(){ + if (options !== undefined && options.disableEventSource) { + return false; + } + + if (!window.EventSource){ + return false; + } + + var eventsourceURL = url.replace('ws:', 'http:').replace('wss:', 'https:'); + var source = new window.EventSource(eventsourceURL); + + source.onopen = function () { + fake.readyState = OPEN; + fake.onopen(); + }; + + source.onmessage = function (event) { + fake.onmessage(event); + }; + + source.onerror = function () { + source.close(); // bullet will handle reconnects + source = undefined; + fake.onerror(); + }; + + var fake = { + readyState: CONNECTING, + send: function(data){ + return false; // fallback to another method instead? + }, + close: function(){ + fake.readyState = CLOSED; + source.close(); + source = undefined; + fake.onclose(); + } + }; + + return {'heart': false, 'transport': function(){ return fake; }}; + }, + xhrPolling: function(){ + if (options !== undefined && options.disableXHRPolling) { + return false; + } + var timeout; var xhr; @@ -216,6 +267,7 @@ if (readyState == CLOSING){ readyState = CLOSED; + transport = false; stream.onclose(); } else{ // Close happened on connect, select next transport |