Permalink
Browse files

Added support for Node 0.1.94

Close timeout for transports with keep-alive / websocket set to 0
Close timeout for xhr-polling reduced from 8 seconds to 5 seconds
Now you can pass options to specific transports from io.listen()
Some cleanup
  • Loading branch information...
1 parent 67815ec commit 0459c9584880ec91024a90b80fa14bbc37047b2c @rauchg rauchg committed May 12, 2010
View
@@ -11,7 +11,7 @@ The `Socket.IO` server provides seamless supports for a variety of transports in
Requirements
------------
-- Node v0.1.93+
+- Node v0.1.94+
- [Socket.IO client](http://github.com/LearnBoost/Socket.IO) to connect from the browser
How to use
@@ -105,11 +105,9 @@ Options:
A list of the accepted transports.
-- *timeout*
-
- 8000
-
- Time it has to pass without a reconnection to consider a client disconnected. Applies to all transports.
+- *transportOptions*
+
+ An object of options to pass to each transport. For example `{ websocket: { closeTimeout: 8000 }}`
- *log*
@@ -171,7 +169,7 @@ One of the design goals is that you should be able to implement whatever protoco
The concept of session also benefits naturally full-duplex WebSocket, in the event of an accidental disconnection and a quick reconnection. Messages that the server intends to deliver to the client are cached temporarily until the reconnection.
- The implementation of reconnection logic (potentially with retries) is left for the user.
+ The implementation of reconnection logic (potentially with retries) is left for the user. By default, transports that are keep-alive or open all the time (like WebSocket) have a timeout of 0 if a disconnection is detected.
* Message batching
View
@@ -1,9 +1,16 @@
-var sys = require('sys');
+var Options = require('./util/options').Options;
this.Client = Class({
+
+ include: [Options],
+
+ options: {
+ closeTimeout: 0
+ },
- init: function(listener, req, res){
- this.listener = listener;
+ init: function(listener, req, res, options){
+ this.listener = listener;
+ this.setOptions(options);
this.connections = 0;
this.connected = false;
this._onConnect(req, res);
@@ -66,7 +73,7 @@ this.Client = Class({
this.connected = false;
this._disconnectTimeout = setTimeout(function(){
self._onDisconnect();
- }, this.listener.options.timeout);
+ }, this.options.closeTimeout);
},
_onDisconnect: function(){
View
@@ -1,9 +1,10 @@
var url = require('url'),
sys = require('sys'),
- Options = require('./util/options').Options,
+ Options = require('./util/options').Options,
Client = require('./client').Client,
- Transports = {},
-
+
+Transports = {};
+
Listener = this.Listener = Class({
include: [process.EventEmitter.prototype, Options],
@@ -12,33 +13,38 @@ Listener = this.Listener = Class({
origins: '*:*',
resource: 'socket.io',
transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling'],
- timeout: 8000,
+ transportOptions: {},
log: function(message){
sys.log(message);
}
},
init: function(server, options){
+ var self = this;
process.EventEmitter.call(this);
-
- this.server = server;
- this.setOptions(options);
+ this.server = server;
+ this.setOptions(options);
this.clients = [];
this.clientsIndex = {};
var listener = (this.server._events['request'] instanceof Array)
? this.server._events['request'][0]
: this.server._events['request'];
if (listener){
- var self = this;
this.server._events['request'] = function(req, res){
if (self.check(req, res)) return;
- listener(req, res);
+ listener(req, res);
};
} else {
throw new Error('Couldn\'t find the `request` event in the HTTP server.');
}
+ this.server.addListener('upgrade', function(req, socket, head){
+ if (!self.check(req, socket, true)){
+ socket.destroy();
+ }
+ });
+
this.options.transports.forEach(function(t){
if (!(t in Transports)){
Transports[t] = require('./transports/' + t)[t];
@@ -53,13 +59,13 @@ Listener = this.Listener = Class({
for (var i = 0, l = this.clients.length; i < l; i++){
if (this.clients[i] && (!except || [].concat(except).indexOf(this.clients[i].sessionId) == -1)){
this.clients[i].send(message);
- }
+ }
}
return this;
},
- check: function(req, res){
- var path = url.parse(req.url).pathname, parts, cn;
+ check: function(req, res, httpUpgrade){
+ var path = url.parse(req.url).pathname, parts, cn;
if (path.indexOf('/' + this.options.resource) === 0){
parts = path.substr(1).split('/');
if (parts[2]){
@@ -71,10 +77,10 @@ Listener = this.Listener = Class({
sys.log('Couldnt find client with session id "' + parts[2] + '"');
}
} else {
- this._onConnection(parts[1], req, res);
- }
+ this._onConnection(parts[1], req, res, httpUpgrade);
+ }
return true;
- }
+ }
return false;
},
@@ -105,13 +111,13 @@ Listener = this.Listener = Class({
},
// new connections (no session id)
- _onConnection: function(transport, req, res){
- if (this.options.transports.indexOf(transport) === -1){
- req.connection.end();
+ _onConnection: function(transport, req, res, httpUpgrade){
+ if (this.options.transports.indexOf(transport) === -1 || (httpUpgrade && !Transports[transport].httpUpgrade)){
+ httpUpgrade ? res.destroy() : req.connection.destroy();
return sys.log('Illegal transport "'+ transport +'"');
}
sys.log('Initializing client with transport "'+ transport +'"');
- new Transports[transport](this, req, res);
+ new Transports[transport](this, req, res, this.options.transportOptions[transport]);
}
});
@@ -3,7 +3,7 @@ var websocket = require('./websocket').websocket,
listeners = [];
this.flashsocket = websocket.extend({});
-
+this.flashsocket.httpUpgrade = true;
this.flashsocket.init = function(listener){
listeners.push(listener);
};
@@ -3,15 +3,15 @@ var Client = require('../client').Client,
this.websocket = Client.extend({
- _onConnect: function(req, res){
+ _onConnect: function(req, socket){
var self = this;
- this.__super__(req, res);
+ this.request = req;
+ this.connection = socket;
this.data = '';
- if (!this.request.upgrade || this.request.headers['upgrade'] !== 'WebSocket' || !this._verifyOrigin(this.request.headers['origin'])){
+ if (this.request.headers['upgrade'] !== 'WebSocket' || !this._verifyOrigin(this.request.headers['origin'])){
this.listener.options.log('WebSocket connection invalid');
this.connection.end();
- return;
}
this.connection.setTimeout(0);
@@ -60,4 +60,6 @@ this.websocket = Client.extend({
this.connection.write('\uffff', 'binary');
}
-});
+});
+
+this.websocket.httpUpgrade = true;
@@ -5,6 +5,7 @@ var Client = require('../client').Client,
this['xhr-polling'] = Client.extend({
options: {
+ closeTimeout: 5000,
duration: 20000
},

0 comments on commit 0459c95

Please sign in to comment.