From 148b6fe476e35e9e8db448d6dc73d57a3b23e800 Mon Sep 17 00:00:00 2001 From: Florent Gallaire Date: Mon, 11 Nov 2013 02:16:50 +0100 Subject: DRY url.replace(). Automatically disable WebSocket when the URL begins with 'http'. Update the README with the new 'http' url syntax. Update the clock example with the new 'http' url syntax. --- priv/bullet.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'priv') diff --git a/priv/bullet.js b/priv/bullet.js index 26a1fc5..162093b 100644 --- a/priv/bullet.js +++ b/priv/bullet.js @@ -37,6 +37,16 @@ var OPEN = 1; var CLOSING = 2; var CLOSED = 3; + var httpURL = url.replace('ws:', 'http:').replace('wss:', 'https:'); + + if (url == httpURL) { + if (options == undefined) { + var options = {'disableWebSocket': true}; + } + else { + options.disableWebSocket = true; + } + } var xhrSend = function(data){ /** @@ -47,13 +57,12 @@ return false; } - var sendUrl = url.replace('ws:', 'http:').replace('wss:', 'https:'); var self = this; $.ajax({ async: false, cache: false, type: 'POST', - url: sendUrl, + url: httpURL, data: data, dataType: 'text', contentType: 'application/x-www-form-urlencoded; charset=utf-8', @@ -106,8 +115,7 @@ return false; } - var eventsourceURL = url.replace('ws:', 'http:').replace('wss:', 'https:'); - var source = new window.EventSource(eventsourceURL); + var source = new window.EventSource(httpURL); source.onopen = function () { fake.readyState = OPEN; @@ -165,12 +173,11 @@ }; function poll(){ - var fakeurl = url.replace('ws:', 'http:').replace('wss:', 'https:'); xhr = $.ajax({ type: 'GET', cache: false, - url: fakeurl, + url: httpURL, dataType: 'text', data: {}, headers: {'X-Socket-Transport': 'xhrPolling'}, -- cgit v1.2.3