diff --git a/lib/io.js b/lib/io.js index b5fb1e75c..63a353fb9 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,24 @@ io.Transport = require('./transport').Transport; + /** + * Default enabled transports + * + * @api public + */ + + io.transports = ['websocket', 'xhr-polling']; + /** * Expose all transports + * + * @api public */ + io.Transport.XHR = require('./transports/xhr').XHR; + 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..a1da5f9dc 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -165,6 +165,20 @@ 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 diff --git a/lib/transports/xhr-polling.js b/lib/transports/xhr-polling.js index 289febeba..1677ce82e 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. @@ -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 * @@ -88,7 +94,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 +151,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 ); diff --git a/lib/util.js b/lib/util.js index 5a5d3bd44..06d66aa0a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -5,7 +5,7 @@ * MIT Licensed */ -(function (exports) { +(function (exports, global) { /** * Utilities namespace. @@ -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); @@ -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 +); 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" 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 () {