Skip to content

Commit

Permalink
Merge branch 'master' of github.com:LearnBoost/socket.io into gc
Browse files Browse the repository at this point in the history
Conflicts:
	lib/manager.js
  • Loading branch information
3rd-Eden committed Aug 4, 2011
2 parents abe142a + a4ec5aa commit f0ef33b
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 50 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
ALL_TESTS = $(shell find test/ -name '*.test.js')

run-tests:
@npm link > /dev/null --local
@./node_modules/.bin/expresso \
-t 3000 \
-I support \
Expand All @@ -17,4 +16,7 @@ test:
test-cov:
@TESTFLAGS=--cov $(MAKE) test

test-leaks:
@ls test/leaks/* | xargs node --expose_debug_as=debug --expose_gc

.PHONY: test
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Next, attach it to a HTTP/HTTPS server. If you're using the fantastic `express`
web framework:

```js
var app = express.createServer();
var app = express.createServer()
, io = io.listen(app);

app.listen(80);
Expand Down
28 changes: 18 additions & 10 deletions lib/manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost <[email protected]>
Expand All @@ -9,9 +8,7 @@
* Module dependencies.
*/

var http = require('http')
, https = require('https')
, fs = require('fs')
var fs = require('fs')
, url = require('url')
, util = require('./util')
, store = require('./store')
Expand Down Expand Up @@ -55,7 +52,7 @@ var parent = module.parent.exports
* @api public
*/

function Manager (server) {
function Manager (server, options) {
this.server = server;
this.namespaces = {};
this.sockets = this.of('');
Expand Down Expand Up @@ -83,6 +80,10 @@ function Manager (server) {
, 'client store expiration': 15
};

for (var i in options) {
this.settings[i] = options[i];
}

this.initStore();

// reset listeners
Expand Down Expand Up @@ -380,6 +381,10 @@ Manager.prototype.onLeave = function (id, room) {
if (index >= 0) {
this.rooms[room].splice(index, 1);
}

if (!this.rooms[room].length) {
delete this.rooms[room];
}
delete this.roomClients[id][room];
}
};
Expand Down Expand Up @@ -438,13 +443,13 @@ Manager.prototype.onClientMessage = function (id, packet) {
*/

Manager.prototype.onClientDisconnect = function (id, reason) {
this.onDisconnect(id);

for (var name in this.namespaces) {
if (this.roomClients[id][name]) {
this.namespaces[name].handleDisconnect(id, reason);
}
}

this.onDisconnect(id);
};

/**
Expand Down Expand Up @@ -476,8 +481,9 @@ Manager.prototype.onDisconnect = function (id, local) {

if (this.roomClients[id]) {
for (var room in this.roomClients[id]) {
this.rooms[room].splice(this.rooms[room].indexOf(id), 1);
this.onLeave(id, room);
}
delete this.roomClients[id]
}

this.store.destroyClient(id, this.get('client store expiration'));
Expand Down Expand Up @@ -792,7 +798,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
var id = self.generateId()
, hs = [
id
, self.get('heartbeat timeout') || ''
, self.enabled('heartbeats') ? self.get('heartbeat timeout') || '' : ''
, self.get('close timeout') || ''
, self.transports(data).join(',')
].join(':');
Expand Down Expand Up @@ -843,7 +849,9 @@ Manager.prototype.handshakeData = function (data) {
return {
headers: data.headers
, address: connectionAddress
, time: date.toString()
, time: (new Date).toString()
, query: data.query
, url: data.request.url
, xdomain: !!data.request.headers.origin
, secure: data.request.connection.secure
, issued: +date
Expand Down
3 changes: 2 additions & 1 deletion lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SocketNamespace.prototype.__defineGetter__('volatile', function () {
* @api public
*/

SocketNamespace.prototype.in = function (room) {
SocketNamespace.prototype.in = SocketNamespace.prototype.to = function (room) {
this.flags.endpoint = this.name + (room ? '/' + room : '');
return this;
};
Expand Down Expand Up @@ -227,6 +227,7 @@ SocketNamespace.prototype.authorization = function (fn) {
SocketNamespace.prototype.handleDisconnect = function (sid, reason) {
if (this.sockets[sid] && this.sockets[sid].readable) {
this.sockets[sid].onDisconnect(reason);
delete this.sockets[sid];
}
};

Expand Down
5 changes: 4 additions & 1 deletion lib/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ exports.clientVersion = client.version;
/**
* Attaches a manager
*
* @param {HTTPServer/Number} a HTTP/S server or a port number to listen on.
* @param {Object} opts to be passed to Manager and/or http server
* @param {Function} callback if a port is supplied
* @api public
*/

Expand Down Expand Up @@ -65,7 +68,7 @@ exports.listen = function (server, options, fn) {
}

// otherwise assume a http/s server
return new exports.Manager(server);
return new exports.Manager(server, options);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Socket.prototype.__defineGetter__('broadcast', function () {
* @api public
*/

Socket.prototype.to = function (room) {
Socket.prototype.to = Socket.prototype.in = function (room) {
this.flags.room = room;
return this;
};
Expand Down
6 changes: 4 additions & 2 deletions lib/stores/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

var crypto = require('crypto')
, Store = require('../store')
, assert = require('assert')
, redis = require('redis');
, assert = require('assert');

/**
* Exports the constructor.
Expand All @@ -25,6 +24,7 @@ Redis.Client = Client;
* Redis store.
* Options:
* - nodeId (fn) gets an id that uniquely identifies this node
* - redis (fn) redis constructor, defaults to redis
* - redisPub (object) options to pass to the pub redis client
* - redisSub (object) options to pass to the sub redis client
* - redisClient (object) options to pass to the general redis client
Expand Down Expand Up @@ -60,6 +60,8 @@ function Redis (opts) {
}
}

var redis = opts.redis || require('redis');

// initialize a pubsub client and a regular client
this.pub = redis.createClient(opts.redisPub);
this.sub = redis.createClient(opts.redisSub);
Expand Down
8 changes: 4 additions & 4 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ Transport.prototype.clearCloseTimeout = function () {
*/

Transport.prototype.setHeartbeatTimeout = function () {
if (!this.heartbeatTimeout) {
if (!this.heartbeatTimeout && this.manager.enabled('heartbeats')) {
var self = this;

this.heartbeatTimeout = setTimeout(function () {
Expand All @@ -279,7 +279,7 @@ Transport.prototype.setHeartbeatTimeout = function () {
*/

Transport.prototype.clearHeartbeatTimeout = function () {
if (this.heartbeatTimeout) {
if (this.heartbeatTimeout && this.manager.enabled('heartbeats')) {
clearTimeout(this.heartbeatTimeout);
this.heartbeatTimeout = null;
this.log.debug('cleared heartbeat timeout for client', this.id);
Expand All @@ -294,7 +294,7 @@ Transport.prototype.clearHeartbeatTimeout = function () {
*/

Transport.prototype.setHeartbeatInterval = function () {
if (!this.heartbeatInterval) {
if (!this.heartbeatInterval && this.manager.enabled('heartbeats')) {
var self = this;

this.heartbeatInterval = setTimeout(function () {
Expand Down Expand Up @@ -398,7 +398,7 @@ Transport.prototype.onMessage = function (packet) {
*/

Transport.prototype.clearHeartbeatInterval = function () {
if (this.heartbeatInterval) {
if (this.heartbeatInterval && this.manager.enabled('heartbeats')) {
clearTimeout(this.heartbeatInterval);
this.heartbeatInterval = null;
this.log.debug('cleared heartbeat interval for client', this.id);
Expand Down
6 changes: 3 additions & 3 deletions lib/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ HTTPTransport.prototype.handleRequest = function (req) {
});

req.on('end', function () {
res.writeHead(200, headers);
res.end('1');

self.onData(self.postEncoded ? qs.parse(buffer).d : buffer);
});

Expand All @@ -65,9 +68,6 @@ HTTPTransport.prototype.handleRequest = function (req) {
headers['Access-Control-Allow-Credentials'] = 'true';
}
}

res.writeHead(200, headers);
res.end('1');
} else {
this.response = req.res;

Expand Down
1 change: 1 addition & 0 deletions lib/transports/jsonp-polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ JSONPPolling.prototype.doWrite = function (data) {
'Content-Type': 'text/javascript; charset=UTF-8'
, 'Content-Length': Buffer.byteLength(data)
, 'Connection': 'Keep-Alive'
, 'X-XSS-Protection': '0'
});

this.response.write(data);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
}
, "dependencies": {
"socket.io-client": "0.7.4"
, "policyfile": "0.0.3"
, "redis": "0.6.5"
, "policyfile": "0.0.4"
, "redis": "0.6.6"
}
, "devDependencies": {
"expresso": "0.7.7"
, "should": "0.0.4"
, "assertvanish": "0.0.3-1"
}
, "main": "index"
, "engines": { "node": ">= 0.4.0" }
Expand Down
54 changes: 54 additions & 0 deletions test/leaks/socket.leaktest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost <[email protected]>
* MIT Licensed
*/

/**
* Test dependencies.
*/

require.paths.unshift(__dirname + '/../../lib');

var assertvanish = require('assertvanish')
, common = require('../common')
, ports = 15800;

function resultCallback (leaks, leakedSocket) {
if (leaks) {
console.error('Leak detected');
process.exit(1);
} else {
console.error('No leaks');
process.exit(0);
}
};

/**
* Test.
*/

var cl = client(++ports);
var io = create(cl);

io.sockets.on('connection', function (socket) {
console.log('connected');

socket.on('disconnect', function() {
console.log("client gone");
setTimeout(gc, 1000);
assertvanish(socket, 2000, {silent: true, callback: resultCallback});
});
});

setTimeout(function() {
cl.handshake(function (sid) {
var ws = websocket(cl, sid);
ws.on('open', function () {
console.log('open!');
setTimeout(function() {
ws.close();
}, 500);
});
});
}, 100);
Loading

0 comments on commit f0ef33b

Please sign in to comment.