Compare commits

...

86 Commits
0.7.4 ... 0.7.8

Author SHA1 Message Date
Guillermo Rauch
65f1399a44 Release 0.7.8 2011-08-08 08:06:30 -07:00
Guillermo Rauch
d86ffcf06d Merge pull request #408 from 3rd-Eden/gc
Garbage collection
2011-08-04 11:50:39 -07:00
Arnout Kazemier
ab5beaff63 Fix 2011-08-04 20:32:49 +02:00
Arnout Kazemier
f0ef33b45f Merge branch 'master' of github.com:LearnBoost/socket.io into gc
Conflicts:
	lib/manager.js
2011-08-04 20:32:43 +02:00
Guillermo Rauch
a4ec5aafa6 Merge pull request #363 from dvv/489bc860d2f050a7925e602a6d16c428f2784b40
small cleanup
2011-07-30 20:32:39 -07:00
Guillermo Rauch
984639ba67 Merge pull request #381 from 3rd-Eden/alias
Added alias for to to in and in to to
2011-07-30 20:27:30 -07:00
Guillermo Rauch
9923c1dee9 Merge branch 'master' of github.com:LearnBoost/socket.io 2011-07-30 20:23:35 -07:00
Guillermo Rauch
1b0a4849df Added test (fixes #380). 2011-07-30 20:21:13 -07:00
Guillermo Rauch
4fc43f322f Changed; make sure sio#listen passes options to both HTTP server and socket.io manager.
Added docs for sio#listen.
2011-07-30 20:20:31 -07:00
Guillermo Rauch
5c0f78ab02 Added options parameter support for Manager constructor. 2011-07-30 20:19:38 -07:00
Guillermo Rauch
d8c7060cc8 Merge pull request #396 from 3rd-Eden/xxs
Potential fix for #372
2011-07-30 18:49:47 -07:00
Guillermo Rauch
b0335b0a61 Merge pull request #339 from 3rd-Eden/333
Don't require redis by default
2011-07-30 18:47:41 -07:00
Guillermo Rauch
a1797ccd4b Merge pull request #337 from 3rd-Eden/handshakeData
Expose path and querystring in handshakeData
2011-07-30 18:47:12 -07:00
Guillermo Rauch
a1c997bc58 Merge pull request #401 from Pita/master
Send response once we got all POST data, not immediately
2011-07-30 18:36:08 -07:00
Guillermo Rauch
0b9b28d251 Merge pull request #409 from 3rd-Eden/bug/407
Fix for #407
2011-07-30 17:59:06 -07:00
Guillermo Rauch
a79b2fa761 Added memory leaks tests. 2011-07-30 17:53:35 -07:00
Guillermo Rauch
195eba74de Added assertvanish dev dependency. 2011-07-30 17:53:22 -07:00
Guillermo Rauch
3edebe5d61 Added test-leaks Makefile task. 2011-07-30 17:53:14 -07:00
Guillermo Rauch
b56389fbc8 Removed auto npm-linking from make test. 2011-07-30 14:46:54 -07:00
Guillermo Rauch
203293db0b Merge pull request #413 from 3rd-Eden/update/package
updated package.json with new build of policyfile and redis
2011-07-30 14:33:09 -07:00
Guillermo Rauch
2b7ea448c4 Merge pull request #416 from 3rd-Eden/test/374
Added testcase to prevent #374 from happening again
2011-07-30 14:32:51 -07:00
Guillermo Rauch
c627f1b7d0 Merge pull request #421 from 3rd-Eden/bug/heartbeat
Make sure that you can disable heart beats
2011-07-30 14:32:34 -07:00
Guillermo Rauch
4c20afd4b7 Merge pull request #346 from ericz/patch-1
Change `;` typo to `,` in first example
2011-07-26 01:48:22 -07:00
Arnout Kazemier
f689434f61 Make sure that you can disable heart beats 2011-07-25 22:12:48 +02:00
Arnout Kazemier
b694ee68c9 Added testcase to prevent #374 from happening again 2011-07-24 13:21:30 +02:00
Guillermo Rauch
8b22ca2ffd Merge pull request #415 from 3rd-Eden/bug/rooms
Bug/rooms
2011-07-24 02:30:36 -07:00
Arnout Kazemier
5c50c4844f fixed missing done() that cuased the suite to timeout 2011-07-24 11:27:43 +02:00
Arnout Kazemier
4fcad6e4bc updated package.json with new build of policyfile and redis 2011-07-23 22:59:32 +02:00
Arnout Kazemier
3b2316e0d8 Fixed rooms memory leak 2011-07-23 22:55:29 +02:00
Arnout Kazemier
a821cce390 Fix for #407 2011-07-22 22:52:20 +02:00
Arnout Kazemier
abe142ac66 Merge branch 'master' of github.com:LearnBoost/socket.io into gc 2011-07-22 21:42:09 +02:00
Arnout Kazemier
5eff0e5ca7 Small clean up, so the the code makes a bit more sense 2011-07-22 21:37:13 +02:00
Guillermo Rauch
c06242efd3 Merge pull request #383 from AD7six/feature/no-duplicate-clients-in-rooms
prevent duplicate references in rooms - probable fix for #379
2011-07-22 07:51:54 -07:00
Peter 'Pita' Martischka
f69f387e1d Send response once we got all POST data, not immediately 2011-07-20 17:29:26 +01:00
Guillermo Rauch
f5c10aec7f Merge pull request #395 from 3rd-Eden/bug/376
Fix for bug #376
2011-07-18 14:12:15 -07:00
Arnout Kazemier
34bd9d9092 Fix for bug #376
Also added unit test to prevent it from happening in the future
2011-07-18 23:06:48 +02:00
Andy Dawson
c30151d03a Remove unused Cl (client) variable.
This test doesn't use any real clients.
2011-07-18 13:22:08 -07:00
Arnout Kazemier
f784c477f0 Potential fix for #372 2011-07-18 22:02:03 +02:00
AD7six
c899c98f31 prevent duplicate references in rooms 2011-07-15 01:13:41 +02:00
Arnout Kazemier
53f0f4d66d Added alias for to to in and in to to 2011-07-14 21:31:55 +02:00
Guillermo Rauch
c826fadb9f Fixed roomClients definition (#378). 2011-07-13 18:30:24 -03:00
Guillermo Rauch
097094cd7a Updated dep [thanks @3rd-Eden] 2011-07-12 05:11:03 -03:00
Guillermo Rauch
c3fa1bf5af Release 0.7.7 2011-07-12 05:05:49 -03:00
Guillermo Rauch
8798cfbced Merge pull request #340 from 3rd-Eden/291
It was emitting uknown room, so the messages where never send
2011-07-12 00:55:49 -07:00
Arnout Kazemier
81d71ebb08 Fixed port number 2011-07-12 09:52:53 +02:00
Guillermo Rauch
074e74c6c5 Added test for emitting to closed clients to prevent regression. 2011-07-12 04:35:07 -03:00
Guillermo Rauch
65b8272724 Merge branch 'master' of github.com:LearnBoost/socket.io 2011-07-12 04:29:56 -03:00
Guillermo Rauch
ca3f3379cb Fixed race condition in redis test. 2011-07-12 04:29:30 -03:00
Guillermo Rauch
faad10baee Changed Transport#end instrumentation. 2011-07-12 04:29:11 -03:00
Guillermo Rauch
d3eac92eaa Leveraged $emit instead of emit internally. 2011-07-12 04:28:55 -03:00
Guillermo Rauch
fb5b9bc0b1 Made tests faster. 2011-07-12 03:42:05 -03:00
Guillermo Rauch
34505071f4 Fixed double disconnect events. 2011-07-12 03:41:43 -03:00
Guillermo Rauch
0a2d0b9d0b Fixed disconnect logic 2011-07-12 03:41:15 -03:00
Guillermo Rauch
4495f5987a Simplified remote events handling in Socket. 2011-07-11 23:51:42 -03:00
Guillermo Rauch
aad29d5d92 Fixed double dispatch handling. 2011-07-11 23:51:24 -03:00
Guillermo Rauch
ad8452035d Fixed; increased timeout. 2011-07-11 23:51:08 -03:00
Arnout Kazemier
b2ffed891b Fixed typo 2011-07-11 00:12:27 -07:00
ericz
27ab98dca4 Fixed typo 2011-07-05 14:26:46 -07:00
Eric Zhang
a8ca11cb47 Change ; typo to , in first example 2011-07-05 14:22:55 -07:00
Arnout Kazemier
d39d1401c4 Fixed broken test suite 2011-07-05 00:59:31 +02:00
Arnout Kazemier
159c75096d Removed console statement 2011-07-05 00:38:24 +02:00
Arnout Kazemier
a70347b15f Fixed #285 2011-07-05 00:37:19 +02:00
Arnout Kazemier
1a2c8aa31f It was emitting uknown room, so the messages where never send fixes #291 2011-07-05 00:17:02 +02:00
Arnout Kazemier
15e1e68cfd Merge branch 'master' of github.com:LearnBoost/socket.io into 333 2011-07-04 22:53:20 +02:00
Arnout Kazemier
167da44211 Fixes #333 2011-07-04 22:51:00 +02:00
Arnout Kazemier
1372838092 Expose path and querystring in handshakeData 2011-07-04 19:26:22 +02:00
Guillermo Rauch
7257e1ec36 Merge pull request #329 from 3rd-Eden/remoteAddress
.address() returned server ip >_<, solved with this patch and adjusted th
2011-07-03 05:43:48 -07:00
Arnout Kazemier
af5960bc28 address.remoteAddress -> address.address just like node's 2011-07-02 19:50:18 +02:00
Arnout Kazemier
206cfdf9c6 Merge branch 'master' of github.com:LearnBoost/socket.io into remoteAddress
Conflicts:
	lib/manager.js
2011-07-02 17:52:09 +02:00
Arnout Kazemier
65d7229079 .address() returned server ip >_<, solved with this patch and adjusted the test
suite
2011-07-02 17:49:32 +02:00
Guillermo Rauch
c18aa40ba6 Merge pull request #328 from coolbloke1324/master
Client IP instead of server IP for connectionAddress.
2011-07-02 08:41:33 -07:00
coolbloke1324
249f33da16 Updated Manager.prototype.handshakeData to provide connection.remoteAddress instead of .address() because .address() was returning server IP and .remoteAddress returns client IP. 2011-07-02 08:30:18 -07:00
Guillermo Rauch
b9a7c8be90 Merge pull request #325 from 3rd-Eden/ssl
See https://github.com/joyent/node/issues/1055
2011-07-01 12:41:02 -07:00
Arnout Kazemier
e4ac72a316 Unfucked space 2011-07-01 21:26:43 +02:00
Arnout Kazemier
dff9cbfe1b See https://github.com/joyent/node/issues/1055 2011-07-01 21:24:51 +02:00
Vladimir Dronnikov
489bc860d2 cleanup 2011-07-01 09:50:37 -07:00
Guillermo Rauch
29a8fff576 Removed transports definition in chat example. 2011-06-30 18:59:52 -03:00
Guillermo Rauch
e66a68f0fa Fixed room cleanup 2011-06-30 18:59:35 -03:00
Guillermo Rauch
4058eacbd4 Fixed; make sure the client is cleaned up after booting. 2011-06-30 18:58:55 -03:00
Guillermo Rauch
8cbd1544b9 Make sure to mark the client as non-open if the connection is closed. 2011-06-30 18:58:29 -03:00
Guillermo Rauch
f302744fec Removed unneeded buffer declarations. 2011-06-30 18:58:06 -03:00
Guillermo Rauch
a3ba4e2c10 Fixed; make sure to clear socket handlers and subscriptions upon transport close. 2011-06-30 18:19:41 -03:00
Guillermo Rauch
a483f9cafd Release 0.7.6 2011-06-30 15:32:22 -03:00
Guillermo Rauch
b43c82f3db Fixed general dispatching when a client has closed. 2011-06-30 14:03:43 -03:00
Guillermo Rauch
84f0099c1d Release 0.7.5 2011-06-30 12:57:44 -03:00
Guillermo Rauch
c6a883c8a9 Fixed dispatching to clients that are disconnected. 2011-06-30 12:56:42 -03:00
20 changed files with 671 additions and 101 deletions

View File

@@ -1,4 +1,54 @@
0.7.8 / 2011-08-08
==================
* Changed; make sure sio#listen passes options to both HTTP server and socket.io manager.
* Added docs for sio#listen.
* Added options parameter support for Manager constructor.
* Added memory leaks tests and test-leaks Makefile task.
* Removed auto npm-linking from make test.
* Make sure that you can disable heartbeats. [3rd-Eden]
* Fixed rooms memory leak [3rd-Eden]
* Send response once we got all POST data, not immediately [Pita]
* Fixed onLeave behavior with missing clientsk [3rd-Eden]
* Prevent duplicate references in rooms.
* Added alias for `to` to `in` and `in` to `to`.
* Fixed roomClients definition.
* Removed dependency on redis for installation without npm [3rd-Eden]
* Expose path and querystring in handshakeData [3rd-Eden]
0.7.7 / 2011-07-12
==================
* Fixed double dispatch handling with emit to closed clients.
* Added test for emitting to closed clients to prevent regression.
* Fixed race condition in redis test.
* Changed Transport#end instrumentation.
* Leveraged $emit instead of emit internally.
* Made tests faster.
* Fixed double disconnect events.
* Fixed disconnect logic
* Simplified remote events handling in Socket.
* Increased testcase timeout.
* Fixed unknown room emitting (GH-291). [3rd-Eden]
* Fixed `address` in handshakeData. [3rd-Eden]
* Removed transports definition in chat example.
* Fixed room cleanup
* Fixed; make sure the client is cleaned up after booting.
* Make sure to mark the client as non-open if the connection is closed.
* Removed unneeded `buffer` declarations.
* Fixed; make sure to clear socket handlers and subscriptions upon transport close.
0.7.6 / 2011-06-30
==================
* Fixed general dispatching when a client has closed.
0.7.5 / 2011-06-30
==================
* Fixed dispatching to clients that are disconnected.
0.7.4 / 2011-06-30
==================

View File

@@ -2,8 +2,8 @@
ALL_TESTS = $(shell find test/ -name '*.test.js')
run-tests:
@npm link > /dev/null --local
@./node_modules/.bin/expresso \
-t 3000 \
-I support \
-I lib \
--serial \
@@ -16,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

View File

@@ -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);

View File

@@ -61,10 +61,6 @@ app.listen(3000, function () {
var io = sio.listen(app)
, nicknames = {};
io.set('transports', [
, 'xhr-polling'
]);
io.sockets.on('connection', function (socket) {
socket.on('user message', function (msg) {
socket.broadcast.emit('user message', socket.nickname, msg);

View File

@@ -1,4 +1,3 @@
/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
@@ -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')
@@ -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('');
@@ -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
@@ -99,6 +100,13 @@ function Manager (server) {
self.handleUpgrade(req, socket, head);
});
server.on('close', function () {
clearInterval(self.gc);
});
// run our private gc every 10 seconds
this.gc = setInterval(this.garbageCollection.bind(this), 10000);
for (var i in transports) {
if (transports[i].init) {
transports[i].init(this);
@@ -324,16 +332,6 @@ Manager.prototype.onOpen = function (id) {
*/
Manager.prototype.onDispatch = function (room, packet, volatile, exceptions) {
// go through the users who have pending buffers
for (var i = 0, l = this.closedA.length; i < l; i++) {
if (this.roomClients[this.closedA[i]][room]) {
if (!~exceptions.indexOf(this.closedA[i])) {
this.closed[this.closedA[i]].push(packet);
}
}
}
// go through room clients
if (this.rooms[room]) {
for (var i = 0, l = this.rooms[room].length; i < l; i++) {
var id = this.rooms[room][i];
@@ -357,15 +355,17 @@ Manager.prototype.onDispatch = function (room, packet, volatile, exceptions) {
Manager.prototype.onJoin = function (id, name) {
if (!this.roomClients[id]) {
this.roomClients[id] = [];
this.roomClients[id] = {};
}
if (!this.rooms[name]) {
this.rooms[name] = [];
}
this.rooms[name].push(id);
this.roomClients[id][name] = true;
if (!~this.rooms[name].indexOf(id)) {
this.rooms[name].push(id);
this.roomClients[id][name] = true;
}
};
/**
@@ -376,7 +376,15 @@ Manager.prototype.onJoin = function (id, name) {
Manager.prototype.onLeave = function (id, room) {
if (this.rooms[room]) {
this.rooms[room].splice(this.rooms[room].indexOf(id), 1);
var index = this.rooms[room].indexOf(id);
if (index >= 0) {
this.rooms[room].splice(index, 1);
}
if (!this.rooms[room].length) {
delete this.rooms[room];
}
delete this.roomClients[id][room];
}
};
@@ -388,6 +396,10 @@ Manager.prototype.onLeave = function (id, room) {
*/
Manager.prototype.onClose = function (id) {
if (this.open[id]) {
delete this.open[id];
}
this.closed[id] = [];
this.closedA.push(id);
@@ -407,7 +419,9 @@ Manager.prototype.onClose = function (id) {
*/
Manager.prototype.onClientDispatch = function (id, packet) {
this.closed[id].push(packet);
if (this.closed[id]) {
this.closed[id].push(packet);
}
};
/**
@@ -434,6 +448,8 @@ Manager.prototype.onClientDisconnect = function (id, reason) {
this.namespaces[name].handleDisconnect(id, reason);
}
}
this.onDisconnect(id);
};
/**
@@ -465,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.indexOf(id), 1);
this.onLeave(id, room);
}
delete this.roomClients[id]
}
this.store.destroyClient(id, this.get('client store expiration'));
@@ -583,9 +600,10 @@ Manager.prototype.handleClient = function (data, req) {
return;
}
var transport = new transports[data.transport](this, data, req);
var transport = new transports[data.transport](this, data, req)
, handshaken = this.handshaken[data.id];
if (this.handshaken[data.id]) {
if (handshaken) {
if (transport.open) {
if (this.closed[data.id] && this.closed[data.id].length) {
transport.payload(this.closed[data.id]);
@@ -601,6 +619,11 @@ Manager.prototype.handleClient = function (data, req) {
this.onConnect(data.id);
this.store.publish('connect', data.id);
// flag as used
delete handshaken.issued;
this.onHandshake(data.id, handshaken);
this.store.publish('handshake', data.id, handshaken);
// initialize the socket for all namespaces
for (var i in this.namespaces) {
var socket = this.namespaces[i].socket(data.id, true);
@@ -775,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(':');
@@ -807,16 +830,31 @@ Manager.prototype.handleHandshake = function (data, req, res) {
*/
Manager.prototype.handshakeData = function (data) {
var connectionAddress = null;
if (data.request.connection.address) {
connectionAddress = data.request.connection.address();
var connection = data.request.connection
, connectionAddress
, date = new Date;
if (connection.remoteAddress) {
connectionAddress = {
address: connection.remoteAddress
, port: connection.remotePort
};
} else if (connection.socket && connection.socket.remoteAddress) {
connectionAddress = {
address: connection.socket.remoteAddress
, port: connection.socket.remotePort
};
}
return {
headers: data.headers
, address: connectionAddress
, time: (new Date).toString()
, time: date.toString()
, query: data.query
, url: data.request.url
, xdomain: !!data.request.headers.origin
, secure: data.request.connection.secure
, issued: +date
};
};
@@ -946,6 +984,8 @@ Manager.prototype.checkRequest = function (req) {
/**
* Declares a socket namespace
*
* @api public
*/
Manager.prototype.of = function (nsp) {
@@ -955,3 +995,26 @@ Manager.prototype.of = function (nsp) {
return this.namespaces[nsp] = new SocketNamespace(this, nsp);
};
/**
* Perform garbage collection on long living objects and properties that cannot
* be removed automatically.
*
* @api private
*/
Manager.prototype.garbageCollection = function () {
// clean up unused handshakes
var ids = Object.keys(this.handshaken)
, i = ids.length
, now = Date.now()
, handshake;
while (i--) {
handshake = this.handshaken[ids[i]];
if ('issued' in handshake && (now - handshake.issued) >= 3E4) {
this.onDisconnect(ids[i]);
}
}
};

View File

@@ -49,7 +49,7 @@ SocketNamespace.prototype.$emit = EventEmitter.prototype.emit;
SocketNamespace.prototype.clients = function (room) {
var room = this.name + (room !== undefined ?
(this.name !== '' ? '/' : '') + room : '');
'/' + room : '');
if (!this.manager.rooms[room]) {
return [];
@@ -108,8 +108,8 @@ SocketNamespace.prototype.__defineGetter__('volatile', function () {
* @api public
*/
SocketNamespace.prototype.in = function (room) {
this.flags.endpoint = (this.name === '' ? '' : (this.name + '/')) + room;
SocketNamespace.prototype.in = SocketNamespace.prototype.to = function (room) {
this.flags.endpoint = this.name + (room ? '/' + room : '');
return this;
};
@@ -181,7 +181,7 @@ SocketNamespace.prototype.send = function (data) {
*/
SocketNamespace.prototype.emit = function (name) {
if (name == 'connection' || name == 'newListener') {
if (name == 'newListener') {
return this.$emit.apply(this, arguments);
}
@@ -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];
}
};
@@ -287,7 +288,7 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
socket.packet({ type: 'connect' });
// emit connection event
self.emit('connection', socket);
self.$emit('connection', socket);
};
switch (packet.type) {
@@ -333,7 +334,7 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
this.manager.onLeave(sessid, this.name);
this.store.publish('leave', sessid, this.name);
socket.emit('disconnect', packet.reason || 'packet');
socket.$emit('disconnect', packet.reason || 'packet');
break;
case 'json':
@@ -343,6 +344,6 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
if (dataAck)
params.push(ack);
socket.emit.apply(socket, params);
socket.$emit.apply(socket, params);
};
};

View File

@@ -15,7 +15,7 @@ var client = require('socket.io-client');
* Version.
*/
exports.version = '0.7.4';
exports.version = '0.7.8';
/**
* Supported protocol version.
@@ -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
*/
@@ -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);
};
/**

View File

@@ -19,22 +19,6 @@ var parser = require('./parser')
exports = module.exports = Socket;
/**
* Reserved event names.
*/
var events = {
message: 1
, connect: 1
, disconnect: 1
, open: 1
, close: 1
, error: 1
, retry: 1
, reconnect: 1
, newListener: 1
};
/**
* Socket constructor.
*
@@ -122,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;
};
@@ -149,7 +133,7 @@ Socket.prototype.setFlags = function () {
Socket.prototype.onDisconnect = function (reason) {
if (!this.disconnected) {
this.emit('disconnect', reason);
this.$emit('disconnect', reason);
this.disconnected = true;
}
};
@@ -162,7 +146,7 @@ Socket.prototype.onDisconnect = function (reason) {
Socket.prototype.join = function (name, fn) {
var nsp = this.namespace.name
, name = (nsp === '' ? '' : (nsp + '/')) + name;
, name = (nsp + '/') + name;
this.manager.onJoin(this.id, name);
this.manager.store.publish('join', this.id, name);
@@ -183,7 +167,7 @@ Socket.prototype.join = function (name, fn) {
Socket.prototype.leave = function (name, fn) {
var nsp = this.namespace.name
, name = (nsp === '' ? '' : (nsp + '/')) + name;
, name = (nsp + '/') + name;
this.manager.onLeave(this.id, name);
this.manager.store.publish('leave', this.id, name);
@@ -293,13 +277,8 @@ Socket.prototype.disconnect = function () {
if (this.manager.transports[this.id] && this.manager.transports[this.id].open) {
this.manager.transports[this.id].onForcedDisconnect();
} else {
if (this.manager.open[this.id]) {
// if the connection is open in a different node
this.manager.store.publish('disconnect-force:' + this.id);
} else {
this.manager.onClientDisconnect(this.id);
this.manager.store.publish('disconnect:' + this.id);
}
this.manager.onClientDisconnect(this.id);
this.manager.store.publish('disconnect:' + this.id);
}
}
@@ -342,7 +321,7 @@ Socket.prototype.$emit = EventEmitter.prototype.emit;
*/
Socket.prototype.emit = function (ev) {
if (events[ev]) {
if (ev == 'newListener') {
return this.$emit.apply(this, arguments);
}

View File

@@ -11,8 +11,7 @@
var crypto = require('crypto')
, Store = require('../store')
, assert = require('assert')
, redis = require('redis');
, assert = require('assert');
/**
* Exports the constructor.
@@ -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
@@ -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);

View File

@@ -28,7 +28,6 @@ function Transport (mng, data, req) {
this.id = data.id;
this.disconnected = false;
this.drained = true;
this.buffer = [];
this.handleRequest(req);
};
@@ -260,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 () {
@@ -280,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);
@@ -295,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 () {
@@ -357,11 +356,13 @@ Transport.prototype.onMessage = function (packet) {
if ('disconnect' == packet.type && packet.endpoint == '') {
this.log.debug('got disconnection packet');
if (current && current.open) {
if (current) {
current.onForcedDisconnect();
} else {
this.store.publish('disconnect-force:' + this.id);
}
return;
}
if (packet.id && packet.ack != 'data') {
@@ -397,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);
@@ -439,6 +440,7 @@ Transport.prototype.close = function () {
Transport.prototype.onClose = function () {
if (this.open) {
this.setCloseTimeout();
this.clearHandlers();
this.open = false;
this.manager.onClose(this.id);
this.store.publish('close', this.id);
@@ -453,7 +455,7 @@ Transport.prototype.onClose = function () {
Transport.prototype.end = function (reason) {
if (!this.disconnected) {
this.log.info('ending socket');
this.log.info('transport end');
var local = this.manager.transports[this.id];
@@ -480,7 +482,6 @@ Transport.prototype.discard = function () {
this.discarded = true;
this.clearTimeouts();
this.clearHandlers();
this.buffer = [];
return this;
};

View File

@@ -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);
});
@@ -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;

View File

@@ -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);

View File

@@ -1,7 +1,7 @@
{
"name": "socket.io"
, "version": "0.7.4"
, "description": "Realtime apps made cross-browser & easy with a WebSocket-like API"
, "version": "0.7.8"
, "description": "Real-time apps made cross-browser & easy with a WebSocket-like API"
, "homepage": "http://socket.io"
, "keywords": ["websocket", "socket", "realtime", "socket.io", "comet", "ajax"]
, "author": "Guillermo Rauch <guillermo@learnboost.com>"
@@ -15,13 +15,14 @@
, "url": "https://github.com/LearnBoost/Socket.IO-node.git"
}
, "dependencies": {
"socket.io-client": "0.7.3"
, "policyfile": "0.0.3"
, "redis": "0.6.0"
"socket.io-client": "0.7.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" }

View File

@@ -180,7 +180,9 @@ client = function (port) {
create = function (cl) {
console.log('');
return io.listen(cl.port);
var manager = io.listen(cl.port);
manager.set('client store expiration', 0);
return manager;
};
/**

View File

@@ -0,0 +1,54 @@
/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
* 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);

View File

@@ -1,4 +1,3 @@
/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
@@ -21,6 +20,22 @@ var sio = require('socket.io')
module.exports = {
'test setting and getting a configuration flag': function (done) {
var port = ++ports
, io = sio.listen(http.createServer());
io.set('a', 'b');
io.get('a').should.eql('b');
var port = ++ports
, io = sio.listen(http.createServer());
io.configure(function () {
io.set('a', 'b');
io.enable('tobi');
});
io.get('a').should.eql('b');
done();
},
@@ -37,11 +52,9 @@ module.exports = {
, io = sio.listen(http.createServer());
io.configure(function () {
io.set('a', 'b');
io.enable('tobi');
});
io.get('a').should.eql('b');
io.enabled('tobi').should.be.true;
done();
@@ -534,6 +547,100 @@ module.exports = {
io.server.close();
done();
});
}
},
'test disabling heartbeats': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port)
, messages = 0
, beat = false
, ws;
io.configure(function () {
io.disable('heartbeats');
io.set('heartbeat interval', .05);
io.set('heartbeat timeout', .05);
io.set('close timeout', .05);
});
io.sockets.on('connection', function (socket) {
setTimeout(function () {
socket.disconnect();
}, io.get('heartbeat timeout') * 1000 + 100);
socket.on('disconnect', function (reason) {
beat.should.be.false;
cl.end();
ws.finishClose();
io.server.close();
done();
});
});
cl.get('/socket.io/{protocol}/', function (res, data) {
res.statusCode.should.eql(200);
data.should.match(/([^:]+)::[\.0-9]+:(.*)/);
cl.handshake(function (sid) {
ws = websocket(cl, sid);
ws.on('message', function (packet) {
if (++messages == 1) {
packet.type.should.eql('connect');
} else if (packet.type == 'heartbeat'){
beat = true;
}
});
});
});
},
'no duplicate room members': function (done) {
var port = ++ports
, io = sio.listen(port);
Object.keys(io.rooms).length.should.equal(0);
io.onJoin(123, 'foo');
io.rooms.foo.length.should.equal(1);
io.onJoin(123, 'foo');
io.rooms.foo.length.should.equal(1);
io.onJoin(124, 'foo');
io.rooms.foo.length.should.equal(2);
io.onJoin(124, 'foo');
io.rooms.foo.length.should.equal(2);
io.onJoin(123, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(1);
io.onJoin(123, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(1);
io.onJoin(124, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(2);
io.onJoin(124, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(2);
io.server.close();
done();
},
'test passing options directly to the Manager through listen': function (done) {
var port = ++ports
, io = sio.listen(port, { resource: '/my resource', custom: 'opt' });
io.get('resource').should.equal('/my resource');
io.get('custom').should.equal('opt');
io.server.close();
done();
}
};

View File

@@ -80,8 +80,8 @@ module.exports = {
fn(null, true);
})
.on('connection', function (socket) {
socket.handshake.address.address.should.equal('127.0.0.1');
socket.handshake.address.port.should.equal(ports);
(!!socket.handshake.address.address).should.be.true;
(!!socket.handshake.address.port).should.be.true;
socket.handshake.headers.host.should.equal('localhost');
socket.handshake.headers.connection.should.equal('keep-alive');
socket.handshake.time.should.match(/GMT/);
@@ -139,5 +139,109 @@ module.exports = {
}
})
});
},
'broadcasting sends and emits on a namespace': function (done) {
var cl = client(++ports)
, io = create(cl)
, calls = 0
, connect = 0
, message = 0
, events = 0
, expected = 5
, ws1
, ws2;
io.of('a')
.on('connection', function (socket){
socket.broadcast.emit('b', 'test');
socket.broadcast.json.emit('json', {foo:'bar'});
socket.broadcast.send('foo');
});
function finish () {
connect.should.equal(2);
message.should.equal(1);
events.should.equal(2);
cl.end();
ws1.finishClose();
ws2.finishClose();
io.server.close();
done();
}
cl.handshake(function (sid) {
ws1 = websocket(cl, sid);
ws1.on('open', function() {
ws1.packet({
type: 'connect'
, endpoint: 'a'
});
});
ws1.on('message', function (data) {
if (data.type === 'connect') {
++connect;
if (++calls === expected) finish();
}
if (data.type === 'message') {
++message;
if (++calls === expected) finish();
}
if (data.type === 'event') {
if (data.name === 'b' || data.name === 'json') ++events;
if (++calls === expected) finish();
}
});
cl.handshake(function (sid) {
ws2 = websocket(cl, sid);
ws2.on('open', function () {
ws2.packet({
type: 'connect'
, endpoint: 'a'
});
});
})
})
},
'joining rooms inside a namespace': function (done) {
var cl = client(++ports)
, io = create(cl)
, calls = 0
, ws;
io.of('/foo').on('connection', function (socket) {
socket.join('foo.bar');
this.in('foo.bar').emit('baz', 'pewpew');
});
cl.handshake(function (sid) {
ws = websocket(cl, sid);
ws.on('open', function (){
ws.packet({
type: 'connect'
, endpoint: '/foo'
});
});
ws.on('message', function (data) {
if (data.type === 'event') {
data.name.should.equal('baz');
cl.end();
ws.finishClose();
io.server.close();
done();
}
});
})
}
};

View File

@@ -233,7 +233,7 @@ module.exports = {
store.destroy();
done();
});
}, 1900);
}, 2000);
});
}

View File

@@ -12,7 +12,7 @@
var sio = require('socket.io')
, should = require('./common')
, parser = sio.parser
, ports = 15400;
, ports = 15800;
/**
* Tests.
@@ -899,6 +899,59 @@ module.exports = {
});
},
'test leaving a room': function (done) {
var port = ++ports
, cl1 = client(port)
, cl2 = client(port)
, io = create(cl1)
, joins = 0
, disconnects = 0;
io.set('close timeout', 0);
io.sockets.on('connection', function (socket) {
socket.join('foo');
io.sockets.clients('foo').should.have.length(++joins);
socket.on('disconnect', function () {
socket.leave('foo');
socket.leave('foo');
socket.leave('foo');
io.sockets.clients('foo').should.have.length(--joins);
if (++disconnects == 2) {
io.server.close();
cl1.end();
cl2.end();
done();
}
})
});
cl1.handshake(function (sid) {
var ws1 = websocket(cl1, sid);
ws1.on('message', function (msg) {
if (!ws1.connected) {
msg.type.should.eql('connect');
ws1.connected = true;
ws1.finishClose();
}
});
});
cl2.handshake(function (sid) {
var ws2 = websocket(cl2, sid);
ws2.on('message', function (msg) {
if (!ws2.connected) {
msg.type.should.eql('connect');
ws2.connected = true;
ws2.finishClose();
}
});
});
},
'test message with broadcast flag': function (done) {
var port = ++ports
, cl1 = client(port)
@@ -1493,8 +1546,8 @@ module.exports = {
, ws;
io.sockets.on('connection', function (socket) {
socket.handshake.address.address.should.equal('127.0.0.1');
socket.handshake.address.port.should.equal(ports);
(!!socket.handshake.address.address).should.be.true;
(!!socket.handshake.address.port).should.be.true;
socket.handshake.headers.host.should.equal('localhost');
socket.handshake.headers.connection.should.equal('keep-alive');
socket.handshake.time.should.match(/GMT/);
@@ -1584,6 +1637,42 @@ module.exports = {
});
},
'test accessing handshake data from sockets on disconnect': function (done) {
var cl = client(++ports)
, io = create(cl)
, ws;
io.sockets.on('connection', function (socket) {
socket.on('disconnect', function () {
(!!socket.handshake.address.address).should.be.true;
(!!socket.handshake.address.port).should.be.true;
socket.handshake.headers.host.should.equal('localhost');
socket.handshake.headers.connection.should.equal('keep-alive');
socket.handshake.time.should.match(/GMT/);
setTimeout(function () {
ws.finishClose();
cl.end();
io.server.close();
done();
}, 10);
});
socket.disconnect();
});
cl.handshake(function (sid) {
ws = websocket(cl, sid);
ws.on('message', function (msg) {
if (!ws.connected) {
msg.type.should.eql('connect');
ws.connected = true;
}
});
});
},
'test for intentional and unintentional disconnects': function (done) {
var cl = client(++ports)
, io = create(cl)
@@ -1642,5 +1731,41 @@ module.exports = {
}
});
});
}
},
'test socket clean up': function (done) {
var cl = client(++ports)
, io = create(cl)
, ws;
io.sockets.on('connection', function (socket) {
var self = this
, id = socket.id;
socket.on('disconnect', function () {
setTimeout(function () {
var available = !!self.sockets[id];
available.should.be.false;
ws.finishClose();
cl.end();
io.server.close();
done();
}, 10);
});
socket.disconnect();
});
cl.handshake(function (sid) {
ws = websocket(cl, sid);
ws.on('message', function (msg) {
if (!ws.connected) {
msg.type.should.eql('connect');
ws.connected = true;
}
});
});
},
};

View File

@@ -2635,4 +2635,82 @@ module.exports = {
});
},
'test emitting to closed clients': function (done) {
var cl = client(++ports)
, cl2 = client(ports)
, io = create(cl)
, connections = 0;
io.configure(function () {
io.set('close timeout', .1);
});
io.sockets.on('connection', function (socket) {
socket.send('a');
});
cl.handshake(function (sid) {
cl.get('/socket.io/{protocol}/xhr-polling/' + sid, function (res, packs) {
res.statusCode.should.equal(200);
packs.should.have.length(1);
packs[0].should.eql({ type: 'message', endpoint: '', data: 'a' });
cl2.handshake(function (sid2) {
cl2.get(
'/socket.io/{protocol}/xhr-polling/' + sid2
, function (res, packs) {
res.statusCode.should.equal(200);
packs.should.have.length(1);
packs[0].should.eql({ type: 'message', endpoint: '', data: 'a' });
io.sockets.emit('woot', 'b');
var total = 2;
cl.get(
'/socket.io/{protocol}/xhr-polling/' + sid
, function (res, packs) {
res.statusCode.should.equal(200);
packs.should.have.length(1);
packs[0].should.eql({
type: 'event'
, endpoint: ''
, name: 'woot'
, args: ['b']
});
--total || finish();
}
);
cl2.get(
'/socket.io/{protocol}/xhr-polling/' + sid2
, function (res, packs) {
res.statusCode.should.equal(200);
packs.should.have.length(1);
packs[0].should.eql({
type: 'event'
, endpoint: ''
, name: 'woot'
, args: ['b']
});
--total || finish();
}
);
function finish () {
cl.end();
cl2.end();
io.server.close();
done();
};
}
);
});
});
});
}
};