Skip to content

Commit f0ef33b

Browse files
committed
Merge branch 'master' of github.com:LearnBoost/socket.io into gc
Conflicts: lib/manager.js
2 parents abe142a + a4ec5aa commit f0ef33b

File tree

14 files changed

+267
-50
lines changed

14 files changed

+267
-50
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
ALL_TESTS = $(shell find test/ -name '*.test.js')
33

44
run-tests:
5-
@npm link > /dev/null --local
65
@./node_modules/.bin/expresso \
76
-t 3000 \
87
-I support \
@@ -17,4 +16,7 @@ test:
1716
test-cov:
1817
@TESTFLAGS=--cov $(MAKE) test
1918

19+
test-leaks:
20+
@ls test/leaks/* | xargs node --expose_debug_as=debug --expose_gc
21+
2022
.PHONY: test

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Next, attach it to a HTTP/HTTPS server. If you're using the fantastic `express`
2020
web framework:
2121

2222
```js
23-
var app = express.createServer();
23+
var app = express.createServer()
2424
, io = io.listen(app);
2525

2626
app.listen(80);

lib/manager.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*!
32
* socket.io-node
43
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
@@ -9,9 +8,7 @@
98
* Module dependencies.
109
*/
1110

12-
var http = require('http')
13-
, https = require('https')
14-
, fs = require('fs')
11+
var fs = require('fs')
1512
, url = require('url')
1613
, util = require('./util')
1714
, store = require('./store')
@@ -55,7 +52,7 @@ var parent = module.parent.exports
5552
* @api public
5653
*/
5754

58-
function Manager (server) {
55+
function Manager (server, options) {
5956
this.server = server;
6057
this.namespaces = {};
6158
this.sockets = this.of('');
@@ -83,6 +80,10 @@ function Manager (server) {
8380
, 'client store expiration': 15
8481
};
8582

83+
for (var i in options) {
84+
this.settings[i] = options[i];
85+
}
86+
8687
this.initStore();
8788

8889
// reset listeners
@@ -380,6 +381,10 @@ Manager.prototype.onLeave = function (id, room) {
380381
if (index >= 0) {
381382
this.rooms[room].splice(index, 1);
382383
}
384+
385+
if (!this.rooms[room].length) {
386+
delete this.rooms[room];
387+
}
383388
delete this.roomClients[id][room];
384389
}
385390
};
@@ -438,13 +443,13 @@ Manager.prototype.onClientMessage = function (id, packet) {
438443
*/
439444

440445
Manager.prototype.onClientDisconnect = function (id, reason) {
441-
this.onDisconnect(id);
442-
443446
for (var name in this.namespaces) {
444447
if (this.roomClients[id][name]) {
445448
this.namespaces[name].handleDisconnect(id, reason);
446449
}
447450
}
451+
452+
this.onDisconnect(id);
448453
};
449454

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

477482
if (this.roomClients[id]) {
478483
for (var room in this.roomClients[id]) {
479-
this.rooms[room].splice(this.rooms[room].indexOf(id), 1);
484+
this.onLeave(id, room);
480485
}
486+
delete this.roomClients[id]
481487
}
482488

483489
this.store.destroyClient(id, this.get('client store expiration'));
@@ -792,7 +798,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
792798
var id = self.generateId()
793799
, hs = [
794800
id
795-
, self.get('heartbeat timeout') || ''
801+
, self.enabled('heartbeats') ? self.get('heartbeat timeout') || '' : ''
796802
, self.get('close timeout') || ''
797803
, self.transports(data).join(',')
798804
].join(':');
@@ -843,7 +849,9 @@ Manager.prototype.handshakeData = function (data) {
843849
return {
844850
headers: data.headers
845851
, address: connectionAddress
846-
, time: date.toString()
852+
, time: (new Date).toString()
853+
, query: data.query
854+
, url: data.request.url
847855
, xdomain: !!data.request.headers.origin
848856
, secure: data.request.connection.secure
849857
, issued: +date

lib/namespace.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ SocketNamespace.prototype.__defineGetter__('volatile', function () {
108108
* @api public
109109
*/
110110

111-
SocketNamespace.prototype.in = function (room) {
111+
SocketNamespace.prototype.in = SocketNamespace.prototype.to = function (room) {
112112
this.flags.endpoint = this.name + (room ? '/' + room : '');
113113
return this;
114114
};
@@ -227,6 +227,7 @@ SocketNamespace.prototype.authorization = function (fn) {
227227
SocketNamespace.prototype.handleDisconnect = function (sid, reason) {
228228
if (this.sockets[sid] && this.sockets[sid].readable) {
229229
this.sockets[sid].onDisconnect(reason);
230+
delete this.sockets[sid];
230231
}
231232
};
232233

lib/socket.io.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ exports.clientVersion = client.version;
3232
/**
3333
* Attaches a manager
3434
*
35+
* @param {HTTPServer/Number} a HTTP/S server or a port number to listen on.
36+
* @param {Object} opts to be passed to Manager and/or http server
37+
* @param {Function} callback if a port is supplied
3538
* @api public
3639
*/
3740

@@ -65,7 +68,7 @@ exports.listen = function (server, options, fn) {
6568
}
6669

6770
// otherwise assume a http/s server
68-
return new exports.Manager(server);
71+
return new exports.Manager(server, options);
6972
};
7073

7174
/**

lib/socket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Socket.prototype.__defineGetter__('broadcast', function () {
106106
* @api public
107107
*/
108108

109-
Socket.prototype.to = function (room) {
109+
Socket.prototype.to = Socket.prototype.in = function (room) {
110110
this.flags.room = room;
111111
return this;
112112
};

lib/stores/redis.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
var crypto = require('crypto')
1313
, Store = require('../store')
14-
, assert = require('assert')
15-
, redis = require('redis');
14+
, assert = require('assert');
1615

1716
/**
1817
* Exports the constructor.
@@ -25,6 +24,7 @@ Redis.Client = Client;
2524
* Redis store.
2625
* Options:
2726
* - nodeId (fn) gets an id that uniquely identifies this node
27+
* - redis (fn) redis constructor, defaults to redis
2828
* - redisPub (object) options to pass to the pub redis client
2929
* - redisSub (object) options to pass to the sub redis client
3030
* - redisClient (object) options to pass to the general redis client
@@ -60,6 +60,8 @@ function Redis (opts) {
6060
}
6161
}
6262

63+
var redis = opts.redis || require('redis');
64+
6365
// initialize a pubsub client and a regular client
6466
this.pub = redis.createClient(opts.redisPub);
6567
this.sub = redis.createClient(opts.redisSub);

lib/transport.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Transport.prototype.clearCloseTimeout = function () {
259259
*/
260260

261261
Transport.prototype.setHeartbeatTimeout = function () {
262-
if (!this.heartbeatTimeout) {
262+
if (!this.heartbeatTimeout && this.manager.enabled('heartbeats')) {
263263
var self = this;
264264

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

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

296296
Transport.prototype.setHeartbeatInterval = function () {
297-
if (!this.heartbeatInterval) {
297+
if (!this.heartbeatInterval && this.manager.enabled('heartbeats')) {
298298
var self = this;
299299

300300
this.heartbeatInterval = setTimeout(function () {
@@ -398,7 +398,7 @@ Transport.prototype.onMessage = function (packet) {
398398
*/
399399

400400
Transport.prototype.clearHeartbeatInterval = function () {
401-
if (this.heartbeatInterval) {
401+
if (this.heartbeatInterval && this.manager.enabled('heartbeats')) {
402402
clearTimeout(this.heartbeatInterval);
403403
this.heartbeatInterval = null;
404404
this.log.debug('cleared heartbeat interval for client', this.id);

lib/transports/http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ HTTPTransport.prototype.handleRequest = function (req) {
5454
});
5555

5656
req.on('end', function () {
57+
res.writeHead(200, headers);
58+
res.end('1');
59+
5760
self.onData(self.postEncoded ? qs.parse(buffer).d : buffer);
5861
});
5962

@@ -65,9 +68,6 @@ HTTPTransport.prototype.handleRequest = function (req) {
6568
headers['Access-Control-Allow-Credentials'] = 'true';
6669
}
6770
}
68-
69-
res.writeHead(200, headers);
70-
res.end('1');
7171
} else {
7272
this.response = req.res;
7373

lib/transports/jsonp-polling.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ JSONPPolling.prototype.doWrite = function (data) {
7070
'Content-Type': 'text/javascript; charset=UTF-8'
7171
, 'Content-Length': Buffer.byteLength(data)
7272
, 'Connection': 'Keep-Alive'
73+
, 'X-XSS-Protection': '0'
7374
});
7475

7576
this.response.write(data);

0 commit comments

Comments
 (0)