From 7e57687ae9b1cea4369d2b443f632d4d921407ae Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Fri, 5 Aug 2011 21:21:30 +0200 Subject: [PATCH 1/7] Fixes #268 , without reconenct branch now <3 --- lib/util.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/util.js b/lib/util.js index 5a5d3bd44..6dac54973 100644 --- a/lib/util.js +++ b/lib/util.js @@ -252,6 +252,8 @@ util.inherit = function (ctor, ctor2) { function f() {}; f.prototype = ctor2.prototype; + + util.merge(ctor, ctor2); ctor.prototype = new f; }; From f4e8abd4ec3ab26bf66acf42201ab6a1d2961a66 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Sun, 7 Aug 2011 19:37:56 +0200 Subject: [PATCH 2/7] Inital checkin on Node support --- lib/io.js | 20 +++++++++++++++++++- lib/socket.js | 13 +++++++++---- lib/transports/websocket.js | 19 +++++++++++++++++-- lib/util.js | 18 +++++++++++++----- 4 files changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/io.js b/lib/io.js index b5fb1e75c..751010731 100644 --- a/lib/io.js +++ b/lib/io.js @@ -94,6 +94,14 @@ io.EventEmitter = process.EventEmitter; + /** + * Expose SocketNamespace + * + * @api private + */ + + io.SocketNamespace = require('./namespace').SocketNamespace; + /** * Expose Transport * @@ -102,12 +110,22 @@ io.Transport = require('./transport').Transport; + /** + * Default enabled transports + * + * @api public + */ + + io.transports = ['websocket']; + /** * Expose all transports + * + * @api public */ io.transports.forEach(function (t) { - //io.Transport[t] = require('./transports/node/' + t); + io.Transport[t] = require('./transports/' + t)[t]; }); /** diff --git a/lib/socket.js b/lib/socket.js index 00016bbcc..c1496c8bc 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -5,7 +5,7 @@ * MIT Licensed */ -(function (exports, io) { +(function (exports, io, global) { /** * Expose constructor. @@ -24,7 +24,7 @@ this.options = { port: 80 , secure: false - , document: document + , document: 'document' in global ? document : false , resource: 'socket.io' , transports: io.transports , 'connect timeout': 10000 @@ -52,7 +52,7 @@ (!this.isXDomain() || io.util.ua.hasCORS)) { var self = this; - io.util.on(window, 'beforeunload', function () { + io.util.on(global, 'beforeunload', function () { self.disconnectSync(); }, false); } @@ -147,7 +147,7 @@ } else { var xhr = io.util.request(); - xhr.open('GET', url); + xhr.open('GET', url, true); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { xhr.onreadystatechange = empty; @@ -335,6 +335,10 @@ */ Socket.prototype.isXDomain = function () { + // if node + return false; + // end node + var locPort = window.location.port || 80; return this.options.host !== document.domain || this.options.port != locPort; }; @@ -509,4 +513,5 @@ })( 'undefined' != typeof io ? io : module.exports , 'undefined' != typeof io ? io : module.parent.exports + , this ); diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index d1a005ed9..8c5c4d072 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -5,7 +5,7 @@ * MIT Licensed */ -(function (exports, io) { +(function (exports, io, global) { /** * Expose constructor. @@ -143,7 +143,7 @@ */ WS.check = function () { - return 'WebSocket' in window && !('__addTask' in WebSocket); + return 'WebSocket' in global && !('__addTask' in WebSocket); }; /** @@ -165,7 +165,22 @@ io.transports.push('websocket'); + // if node + var WebSocket = require('websocket-client').WebSocket; + + /** + * It's available + * + * @api public + */ + + WS.check = function () { + return true; + }; + // end node + })( 'undefined' != typeof io ? io.Transport : module.exports , 'undefined' != typeof io ? io : module.parent.exports + , this ); diff --git a/lib/util.js b/lib/util.js index 5a5d3bd44..3c37d9565 100644 --- a/lib/util.js +++ b/lib/util.js @@ -5,7 +5,7 @@ * MIT Licensed */ -(function (exports) { +(function (exports, global) { /** * Utilities namespace. @@ -125,11 +125,11 @@ var pageLoaded = false; util.load = function (fn) { - if (document.readyState === 'complete' || pageLoaded) { + if ('document' in global && document.readyState === 'complete' || pageLoaded) { return fn(); } - util.on(window, 'load', fn, false); + util.on(global, 'load', fn, false); }; /** @@ -141,7 +141,7 @@ util.on = function (element, event, fn, capture) { if (element.attachEvent) { element.attachEvent('on' + event, fn); - } else { + } else if (element.addEventListener) { element.addEventListener(event, fn, capture); } }; @@ -155,6 +155,11 @@ */ util.request = function (xdomain) { + // if node + var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; + return new XMLHttpRequest(); + // end node + if ('undefined' != typeof window) { if (xdomain && window.XDomainRequest) { return new XDomainRequest(); @@ -355,4 +360,7 @@ util.ua.webkit = 'undefined' != typeof navigator && /webkit/i.test(navigator.userAgent); -})('undefined' != typeof window ? io : module.exports); +})( + 'undefined' != typeof window ? io : module.exports + , this +); From f32832fed3b5f8c42a1b1d36a059be246e0270d7 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Sun, 7 Aug 2011 20:30:09 +0200 Subject: [PATCH 3/7] Added xhr-transport support on node --- lib/io.js | 4 +++- lib/transports/websocket.js | 5 ++--- lib/transports/xhr-polling.js | 5 +++-- lib/transports/xhr.js | 9 +++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/io.js b/lib/io.js index 751010731..63a353fb9 100644 --- a/lib/io.js +++ b/lib/io.js @@ -116,7 +116,7 @@ * @api public */ - io.transports = ['websocket']; + io.transports = ['websocket', 'xhr-polling']; /** * Expose all transports @@ -124,6 +124,8 @@ * @api public */ + io.Transport.XHR = require('./transports/xhr').XHR; + io.transports.forEach(function (t) { io.Transport[t] = require('./transports/' + t)[t]; }); diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 8c5c4d072..a1da5f9dc 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -5,7 +5,7 @@ * MIT Licensed */ -(function (exports, io, global) { +(function (exports, io) { /** * Expose constructor. @@ -143,7 +143,7 @@ */ WS.check = function () { - return 'WebSocket' in global && !('__addTask' in WebSocket); + return 'WebSocket' in window && !('__addTask' in WebSocket); }; /** @@ -182,5 +182,4 @@ })( 'undefined' != typeof io ? io.Transport : module.exports , 'undefined' != typeof io ? io : module.parent.exports - , this ); diff --git a/lib/transports/xhr-polling.js b/lib/transports/xhr-polling.js index 289febeba..4e59025b9 100644 --- a/lib/transports/xhr-polling.js +++ b/lib/transports/xhr-polling.js @@ -5,7 +5,7 @@ * MIT Licensed */ -(function (exports, io) { +(function (exports, io, global) { /** * Expose constructor. @@ -88,7 +88,7 @@ this.xhr = this.request(); - if (window.XDomainRequest && this.xhr instanceof XDomainRequest) { + if (global.XDomainRequest && this.xhr instanceof XDomainRequest) { this.xhr.onload = this.xhr.onerror = onload; } else { this.xhr.onreadystatechange = stateChange; @@ -145,4 +145,5 @@ })( 'undefined' != typeof io ? io.Transport : module.exports , 'undefined' != typeof io ? io : module.parent.exports + , this ); diff --git a/lib/transports/xhr.js b/lib/transports/xhr.js index b01ae4408..40b33f535 100644 --- a/lib/transports/xhr.js +++ b/lib/transports/xhr.js @@ -5,7 +5,7 @@ * MIT Licensed */ -(function (exports, io) { +(function (exports, io, global) { /** * Expose constructor. @@ -117,7 +117,7 @@ this.sendXHR = this.request('POST'); - if (window.XDomainRequest && this.sendXHR instanceof XDomainRequest) { + if (global.XDomainRequest && this.sendXHR instanceof XDomainRequest) { this.sendXHR.onload = this.sendXHR.onerror = onload; } else { this.sendXHR.onreadystatechange = stateChange; @@ -151,7 +151,7 @@ var req = io.util.request(this.socket.isXDomain()) , query = io.util.query(this.socket.options.query, + 't=' + +new Date); - req.open(method || 'GET', this.prepareUrl() + query); + req.open(method || 'GET', this.prepareUrl() + query, true); if (method == 'POST') { try { @@ -194,7 +194,7 @@ return false; }; - + /** * Check if the XHR transport supports corss domain requests. * @@ -209,4 +209,5 @@ })( 'undefined' != typeof io ? io.Transport : module.exports , 'undefined' != typeof io ? io : module.parent.exports + , this ); From e0d3bdb4acbe993a058618e1339b8cee7acd93d3 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Sun, 7 Aug 2011 21:27:11 +0200 Subject: [PATCH 4/7] We decided to follow the inherit of node. So we only merge it for transport that need it --- lib/transports/xhr-polling.js | 6 ++++++ lib/util.js | 2 -- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/transports/xhr-polling.js b/lib/transports/xhr-polling.js index 289febeba..1d05fe117 100644 --- a/lib/transports/xhr-polling.js +++ b/lib/transports/xhr-polling.js @@ -31,6 +31,12 @@ io.util.inherit(XHRPolling, io.Transport.XHR); + /** + * Merge the properties from XHR transport + */ + + io.util.merge(XHRPolling, io.Transport.XHR); + /** * Transport name * diff --git a/lib/util.js b/lib/util.js index 6dac54973..5a5d3bd44 100644 --- a/lib/util.js +++ b/lib/util.js @@ -252,8 +252,6 @@ util.inherit = function (ctor, ctor2) { function f() {}; f.prototype = ctor2.prototype; - - util.merge(ctor, ctor2); ctor.prototype = new f; }; From f544d5fb4398cd60be9f38104ea2b8e9f8397da1 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Sun, 7 Aug 2011 23:30:20 +0200 Subject: [PATCH 5/7] Completed refactor of #240 Fixes #208 Updated package.json Fixed ineffecient document check --- lib/util.js | 2 +- package.json | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 58e19830d..fed08c90e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -52,7 +52,7 @@ , host = uri.host , port = uri.port; - if ('undefined' != typeof document) { + if (!'document' in global) { host = host || document.domain; port = port || (protocol == 'https' && document.location.protocol !== 'https:' ? 443 : document.location.port); diff --git a/package.json b/package.json index 0fa28f668..2a6a4e7d0 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,8 @@ } , "dependencies": { "uglify-js": "1.0.6" + , "websocket-client": "1.0.0" + , "xmlhttprequest": "1.2.2" } , "devDependencies": { "expresso": "0.7.7" From 00a8c52cc1e48ebac3db7907b58217609b2ce597 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Sun, 7 Aug 2011 23:38:44 +0200 Subject: [PATCH 6/7] I did one undo to much before saving it. Fixed uniqueURI again --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index fed08c90e..5d4f7d3c6 100644 --- a/lib/util.js +++ b/lib/util.js @@ -52,7 +52,7 @@ , host = uri.host , port = uri.port; - if (!'document' in global) { + if ('document' in global) { host = host || document.domain; port = port || (protocol == 'https' && document.location.protocol !== 'https:' ? 443 : document.location.port); From 9ad05ab6d2d54642156a003c79248211cf6b6558 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Sun, 7 Aug 2011 23:48:52 +0200 Subject: [PATCH 7/7] Passes test --- test/util.test.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/util.test.js b/test/util.test.js index 8e52e1ecf..30db5a63c 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -62,12 +62,8 @@ }, 'request': function () { - if ('undefined' == typeof window) { - should.equal(io.util.request(), null); - } else { - var type = typeof io.util.request(); - type.should().eql('object'); - } + var type = typeof io.util.request(); + type.should().eql('object'); }, 'is array': function () {