Skip to content

Commit

Permalink
Merge pull request socketio#274 from 3rd-Eden/node
Browse files Browse the repository at this point in the history
Node support
  • Loading branch information
rauchg committed Aug 7, 2011
2 parents 7fbf340 + ef8be0e commit 95c8b66
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 23 deletions.
22 changes: 21 additions & 1 deletion lib/io.js
Expand Up @@ -94,6 +94,14 @@

io.EventEmitter = process.EventEmitter;

/**
* Expose SocketNamespace
*
* @api private
*/

io.SocketNamespace = require('./namespace').SocketNamespace;

/**
* Expose Transport
*
Expand All @@ -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];
});

/**
Expand Down
13 changes: 9 additions & 4 deletions lib/socket.js
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

(function (exports, io) {
(function (exports, io, global) {

/**
* Expose constructor.
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -509,4 +513,5 @@
})(
'undefined' != typeof io ? io : module.exports
, 'undefined' != typeof io ? io : module.parent.exports
, this
);
14 changes: 14 additions & 0 deletions lib/transports/websocket.js
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/transports/xhr-polling.js
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

(function (exports, io) {
(function (exports, io, global) {

/**
* Expose constructor.
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -145,4 +151,5 @@
})(
'undefined' != typeof io ? io.Transport : module.exports
, 'undefined' != typeof io ? io : module.parent.exports
, this
);
9 changes: 5 additions & 4 deletions lib/transports/xhr.js
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

(function (exports, io) {
(function (exports, io, global) {

/**
* Expose constructor.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -194,7 +194,7 @@

return false;
};

/**
* Check if the XHR transport supports corss domain requests.
*
Expand All @@ -209,4 +209,5 @@
})(
'undefined' != typeof io ? io.Transport : module.exports
, 'undefined' != typeof io ? io : module.parent.exports
, this
);
20 changes: 14 additions & 6 deletions lib/util.js
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

(function (exports) {
(function (exports, global) {

/**
* Utilities namespace.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
};

/**
Expand All @@ -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);
}
};
Expand All @@ -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();
Expand Down Expand Up @@ -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
);
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -18,6 +18,8 @@
}
, "dependencies": {
"uglify-js": "1.0.6"
, "websocket-client": "1.0.0"
, "xmlhttprequest": "1.2.2"
}
, "devDependencies": {
"expresso": "0.7.7"
Expand Down
8 changes: 2 additions & 6 deletions test/util.test.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 95c8b66

Please sign in to comment.