Compare commits

..

55 Commits

Author SHA1 Message Date
Roman Shtylman
213577204a Merge branch 'dynamic-namespaces' of https://github.com/markitx/socket.io into dynamic-namespace
Conflicts:
	lib/client.js
	test/socket.io.js
2015-02-04 18:17:49 -08:00
Dylan Lingelbach
dd72676c25 Make name more descriptive 2015-02-04 13:43:21 -06:00
Dylan Lingelbach
3bf7be73a0 Style cleanup 2015-02-04 13:40:40 -06:00
Dylan Lingelbach
0360553c99 Merge branch 'master' into dynamic-namespaces 2015-02-04 13:10:40 -06:00
Dylan Lingelbach
de5cbdb833 Improve tests 2015-02-04 12:33:18 -06:00
Guillermo Rauch
a93d05a9f3 package: bump parser 2015-02-03 16:30:49 -08:00
Guillermo Rauch
5ce06d3088 socket: warn node_redis-style about missing error 2015-02-03 16:26:21 -08:00
Guillermo Rauch
816bfec783 added failing tests 2015-02-03 16:26:08 -08:00
Guillermo Rauch
0a17c90d7a test: added failing test 2015-02-03 15:10:46 -08:00
Guillermo Rauch
3645741b86 test: increase timeout for large binary data test 2015-01-31 09:35:37 -08:00
Guillermo Rauch
f2a7322b5a update deps 2015-01-31 08:46:32 -08:00
Guillermo Rauch
97c6568f65 Merge pull request #1966 from cha0s/clients
Suggestion for implementation of clients API
2015-01-31 08:34:43 -08:00
Ruben Rodriguez II
8814825a35 Suggestion for implementation of clients API 2015-01-31 02:17:49 -06:00
Guillermo Rauch
58eaecad27 Merge branch 'master' of github.com:Automattic/socket.io 2015-01-29 20:39:00 -08:00
Guillermo Rauch
94157e650e point to master 2015-01-29 20:38:36 -08:00
Guillermo Rauch
0935b81da2 Merge pull request #1963 from michael-luo/bugfix/1956
bugfix/1956 test for don't reuse same-namespace connections
2015-01-29 20:34:38 -08:00
Michael Luo
afa871bb8a bugfix/1956 test for don't reuse same-namspace connections 2015-01-24 20:19:16 -08:00
Guillermo Rauch
1b01e16a6c fix broken previous commit 2015-01-24 08:02:05 -08:00
Guillermo Rauch
bd6f638c8f package: bump to work with all objects (fixes #1955) 2015-01-24 07:06:30 -08:00
Guillermo Rauch
83b36e54ac Merge pull request #1958 from nkzawa/compress
Support compression
2015-01-23 10:04:43 -08:00
Guillermo Rauch
2f0d9d05af fix origin verification default https port [evanlucas]
for PR #1693
2015-01-22 21:41:59 +00:00
Naoyuki Kanezawa
429eb0cb7c point to the specific commit 2015-01-22 05:26:37 +09:00
Naoyuki Kanezawa
ac8e8598d7 support compression 2015-01-22 05:01:45 +09:00
Guillermo Rauch
9ba6d47ec7 Release 1.3.2 2015-01-19 15:14:18 +00:00
Guillermo Rauch
3d49cafd03 Release 1.3.1 2015-01-19 11:37:14 +00:00
Guillermo Rauch
77ca2dcbda package: bump engine.io (noop) 2015-01-19 11:11:12 +00:00
Guillermo Rauch
7e4aa4fa64 Release 1.3.0 2015-01-19 10:25:49 +00:00
Guillermo Rauch
b46e480f65 Update Readme.md 2015-01-19 09:26:12 +00:00
Guillermo Rauch
5e92dd8663 package: bump socket.io-client 2015-01-18 19:42:43 +00:00
Guillermo Rauch
f981d3f050 package: bump engine.io 2015-01-18 19:41:45 +00:00
Guillermo Rauch
f57505fee7 Merge branch 'master' of github.com:Automattic/socket.io 2015-01-18 18:53:17 +00:00
Guillermo Rauch
f8f1b132a1 package: bump socket.io-client 2015-01-18 18:52:06 +00:00
Guillermo Rauch
f7f83bc09f package: bump engine.io 2015-01-18 18:51:27 +00:00
Guillermo Rauch
b8ded0d725 Merge pull request #1903 from rase-/add/volatile-tests
Added tests for volatile
2015-01-17 12:23:06 +00:00
Guillermo Rauch
086ccd2708 Merge pull request #1938 from fay-jai/license
update license with up-to-date year range
2015-01-17 12:22:11 +00:00
Guillermo Rauch
864857cb6f Merge pull request #1939 from rase-/add/test-for-emit-after-server-restart
Add test for reconnection after server restarts
2015-01-17 12:21:53 +00:00
Guillermo Rauch
e5a7e422f9 Merge pull request #1943 from eychu/patch-1
Add space in error message
2015-01-17 12:21:38 +00:00
Alexey Chuvashov
f5b75151bd Add space in error message 2015-01-15 01:24:19 +03:00
Tony Kovanen
0523b655da Add test for reconnection after server restarts 2015-01-14 17:22:50 +02:00
Willson Mock
d9415a38e4 update license with up-to-date year range 2015-01-11 00:11:55 -05:00
Roman Shtylman
ca82c09bf2 fix leaving unknown rooms
close #1670
2015-01-10 14:58:50 -08:00
Roman Shtylman
b9aaa1607c Merge pull request #1922 from smart--petea/patch-1
Update Readme.md
2015-01-10 14:48:59 -08:00
Roman Shtylman
d1304c5d82 Merge pull request #1926 from coderaiser/patch-1
package: debug v2.1.0
2015-01-10 14:33:21 -08:00
Guillermo Rauch
bd479a9cd6 Merge pull request #1936 from drewblaisdell/master
Allow null or undefined origins when allowed origins is a function
2015-01-09 16:36:38 -08:00
Drew Blaisdell
a116d7765a Allow null or undefined origins when allowed origins is a function
Requests without an Origin header previously caused an exception to be thrown if the allowed origins passed to the constructor was set to a dynamic function. Omitted origins are now set to an asterisk and passed properly to the origins function.

A test for this case is included in this commit.
2015-01-09 16:30:08 -08:00
Guillermo Rauch
1c6620d564 everything is fixed 2015-01-09 16:25:42 -08:00
Guillermo Rauch
dba462e6da fix tests on 0.11 2015-01-09 16:24:24 -08:00
Guillermo Rauch
19c4422361 package: fix npm test to run on windows 2015-01-09 13:57:45 -08:00
Dylan Lingelbach
0ce0ce1dbc Merge pull request #1 from markitx/dynamic-namespaces
Dynamic namespaces
2015-01-09 11:56:51 -06:00
coderaiser
8242dd01ef package: debug v2.1.0 2014-12-26 14:16:27 +02:00
Badarau Petru
17960ed038 Update Readme.md
underylings to underlyings
2014-12-22 07:17:01 +02:00
Dylan Lingelbach
cc7ce79251 Ensure server sends connect message 2014-12-19 15:32:20 -06:00
Dylan Lingelbach
1b77c27f7b Fix test 2014-12-19 15:27:08 -06:00
Dylan Lingelbach
1f8bb8a0ec Support dynamic namespaces 2014-12-19 13:22:41 -06:00
Tony Kovanen
d9996f0470 Added tests for volatile 2014-12-08 11:19:49 +02:00
9 changed files with 766 additions and 47 deletions

View File

@@ -1,4 +1,28 @@
1.3.2 / 2015-01-19
==================
* no change on this release
1.3.1 / 2015-01-19
==================
* no change on this release
* package: bump `engine.io`
1.3.0 / 2015-01-19
==================
* package: bump `engine.io`
* add test for reconnection after server restarts [rase-]
* update license with up-to-date year range [fay-jai]
* fix leaving unknown rooms [defunctzombie]
* allow null origins when allowed origins is a function [drewblaisdell]
* fix tests on node 0.11
* package: fix `npm test` to run on windows
* package: bump `debug` v2.1.0 [coderaiser]
* added tests for volatile [rase-]
1.2.1 / 2014-11-21
==================

View File

@@ -1,6 +1,6 @@
(The MIT License)
Copyright (c) 2014 Automattic <dev@cloudup.com>
Copyright (c) 2014-2015 Automattic <dev@cloudup.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@@ -2,8 +2,8 @@
# socket.io
[![Build Status](https://secure.travis-ci.org/Automattic/socket.io.svg)](http://travis-ci.org/Automattic/socket.io)
[![NPM version](https://badge.fury.io/js/socket.io.svg)](http://badge.fury.io/js/socket.io)
![Downloads](http://img.shields.io/npm/dm/socket.io.svg)
![NPM version](https://badge.fury.io/js/socket.io.svg)
![Downloads](http://img.shields.io/npm/dm/socket.io.svg?style=flat)
## How to use
@@ -240,6 +240,40 @@ server.listen(3000);
Hash of `Socket` objects that are connected to this namespace indexed
by `id`.
### Namespace#clients(fn:Function)
Gets a list of client IDs connected to this namespace (across all nodes if applicable).
An example to get all clients in a namespace:
```js
var io = require('socket.io')();
io.of('/chat').clients(function(error, clients){
if (error) throw error;
console.log(clients); // => [PZDoMHjiu8PYfRiKAAAF, Anw2LatarvGVVXEIAAAD]
});
```
An example to get all clients in namespace's room:
```js
var io = require('socket.io')();
io.of('/chat').in('general').clients(function(error, clients){
if (error) throw error;
console.log(clients); // => [Anw2LatarvGVVXEIAAAD]
});
```
As with broadcasting, the default is all clients from the default namespace ('/'):
```js
var io = require('socket.io')();
io.clients(function(error, clients){
if (error) throw error;
console.log(clients); // => [6em3d4TJP8Et9EMNAAAA, G5p55dHhGgUnLUctAAAB]
});
```
### Namespace#use(fn:Function):Namespace
Registers a middleware, which is a function that gets executed for
@@ -274,7 +308,7 @@ server.listen(3000);
### Socket#conn:Socket
A reference to the underyling `Client` transport connection (engine.io
A reference to the underlying `Client` transport connection (engine.io
`Socket` object).
### Socket#request:Request
@@ -341,6 +375,18 @@ server.listen(3000);
});
```
### Socket#compress(v:Boolean):Socket
Sets a modifier for a subsequent event emission that the event data will
only be _compressed_ if the value is `true`. Defaults to `true` when you don't call the method.
```js
var io = require('socket.io')();
io.on('connection', function(socket){
socket.compress(false).emit('an event', { some: 'data' });
});
```
### Client
The `Client` class represents an incoming transport (engine.io)

View File

@@ -59,26 +59,40 @@ Client.prototype.setup = function(){
*/
Client.prototype.connect = function(name){
debug('connecting to namespace %s', name);
if (!this.server.nsps[name]) {
this.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'});
return;
}
var nsp = this.server.of(name);
if ('/' != name && !this.nsps['/']) {
this.connectBuffer.push(name);
return;
}
var self = this;
var socket = nsp.add(this, function(){
self.sockets.push(socket);
self.nsps[nsp.name] = socket;
debug('connecting to namespace %s', name);
if ('/' == nsp.name && self.connectBuffer.length > 0) {
self.connectBuffer.forEach(self.connect, self);
self.connectBuffer = [];
function connectNamespace() {
var nsp = self.server.of(name);
if ('/' != name && !self.nsps['/']) {
self.connectBuffer.push(name);
return;
}
var socket = nsp.add(self, function(){
self.sockets.push(socket);
self.nsps[nsp.name] = socket;
if ('/' == nsp.name && self.connectBuffer.length > 0) {
self.connectBuffer.forEach(self.connect, self);
self.connectBuffer = [];
}
});
}
if (self.server.nsps[name]) {
// Namespace already created, connect
connectNamespace();
return;
}
self.server.checkNamespace(name, function(allow) {
if (allow) {
connectNamespace();
return
}
self.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'});
});
};
@@ -135,17 +149,18 @@ Client.prototype.close = function(){
* @param {Object} packet object
* @param {Boolean} whether packet is already encoded
* @param {Boolean} whether packet is volatile
* @param {Boolean} whether packet should be compressed
* @api private
*/
Client.prototype.packet = function(packet, preEncoded, volatile){
Client.prototype.packet = function(packet, preEncoded, volatile, compress){
var self = this;
// this writes to the actual connection
function writeToEngine(encodedPackets) {
if (volatile && !self.conn.transport.writable) return;
for (var i = 0; i < encodedPackets.length; i++) {
self.conn.write(encodedPackets[i]);
self.conn.write(encodedPackets[i], { compress: compress });
}
}

View File

@@ -43,6 +43,7 @@ function Server(srv, opts){
}
opts = opts || {};
this.nsps = {};
this.nspValidators = [];
this.path(opts.path || '/socket.io');
this.serveClient(false !== opts.serveClient);
this.adapter(opts.adapter || Adapter);
@@ -62,14 +63,17 @@ Server.prototype.checkRequest = function(req, fn) {
var origin = req.headers.origin || req.headers.referer;
// file:// URLs produce a null Origin which can't be authorized via echo-back
if ('null' == origin) origin = '*';
if ('null' == origin || null == origin) origin = '*';
if (!!origin && typeof(this._origins) == 'function') return this._origins(origin, fn);
if (this._origins.indexOf('*:*') !== -1) return fn(null, true);
if (origin) {
try {
var parts = url.parse(origin);
parts.port = parts.port || 80;
var defaultPort = 'https:' == parts.protocol ? 443 : 80;
parts.port = parts.port != null
? parts.port
: defaultPort;
var ok =
~this._origins.indexOf(parts.hostname + ':' + parts.port) ||
~this._origins.indexOf(parts.hostname + ':*') ||
@@ -134,6 +138,53 @@ Server.prototype.set = function(key, val){
return this;
};
/**
* Sets up server middleware to validate incoming namespaces not already created on the server.
*
* @return {Server} self
* @api public
*/
Server.prototype.useNamespaceValidator = function(fn){
this.nspValidators.push(fn);
return this;
};
/**
* Executes the middleware for an incoming namespace not already created on the server.
*
* @param name of incomming namespace
* @param {Function} last fn call in the middleware
* @api private
*/
Server.prototype.checkNamespace = function(name, fn){
var fns = this.nspValidators.slice(0);
if (!fns.length) return fn(false);
var namespaceAllowed = false; // Deny unknown namespaces by default
function run(i){
fns[i](name, function(err, allow){
// upon error, short-circuit
if (err) return fn(false);
// if one piece of middleware explicitly denies namespace, short-circuit
if (allow === false) return fn(false);
namespaceAllowed = namespaceAllowed || allow === true;
// if no middleware left, summon callback
if (!fns[i + 1]) return fn(namespaceAllowed);
// go on to next
run(i + 1);
});
}
run(0);
};
/**
* Sets the client serving path.
*
@@ -194,7 +245,7 @@ Server.prototype.origins = function(v){
Server.prototype.listen =
Server.prototype.attach = function(srv, opts){
if ('function' == typeof srv) {
var msg = 'You are trying to attach socket.io to an express' +
var msg = 'You are trying to attach socket.io to an express ' +
'request handler function. Please pass a http.Server instance.';
throw new Error(msg);
}
@@ -251,7 +302,7 @@ Server.prototype.attachServe = function(srv){
var self = this;
srv.removeAllListeners('request');
srv.on('request', function(req, res) {
if (0 == req.url.indexOf(url)) {
if (0 === req.url.indexOf(url)) {
self.serve(req, res);
} else {
for (var i = 0; i < evs.length; i++) {
@@ -358,7 +409,7 @@ Server.prototype.close = function(){
* Expose main namespace (/).
*/
['on', 'to', 'in', 'use', 'emit', 'send', 'write'].forEach(function(fn){
['on', 'to', 'in', 'use', 'emit', 'send', 'write', 'clients'].forEach(function(fn){
Server.prototype[fn] = function(){
var nsp = this.sockets[fn];
return nsp.apply(this.sockets, arguments);

View File

@@ -7,7 +7,7 @@ var Socket = require('./socket');
var Emitter = require('events').EventEmitter;
var parser = require('socket.io-parser');
var debug = require('debug')('socket.io:namespace');
var hasBin = require('has-binary-data');
var hasBin = require('has-binary');
/**
* Module exports.
@@ -240,3 +240,15 @@ Namespace.prototype.write = function(){
this.emit.apply(this, args);
return this;
};
/**
* Gets a list of clients.
*
* @return {Namespace} self
* @api public
*/
Namespace.prototype.clients = function(fn){
this.adapter.clients(this.rooms, fn);
return this;
};

View File

@@ -7,7 +7,7 @@ var Emitter = require('events').EventEmitter;
var parser = require('socket.io-parser');
var url = require('url');
var debug = require('debug')('socket.io:socket');
var hasBin = require('has-binary-data');
var hasBin = require('has-binary');
/**
* Module exports.
@@ -202,7 +202,8 @@ Socket.prototype.write = function(){
Socket.prototype.packet = function(packet, preEncoded){
packet.nsp = this.nsp.name;
var volatile = this.flags && this.flags.volatile;
this.client.packet(packet, preEncoded, volatile);
var compress = !this.flags || false !== this.flags.compress;
this.client.packet(packet, preEncoded, volatile, compress);
};
/**
@@ -242,7 +243,10 @@ Socket.prototype.leave = function(room, fn){
this.adapter.del(this.id, room, function(err){
if (err) return fn && fn(err);
debug('left room %s', room);
self.rooms.splice(self.rooms.indexOf(room), 1);
var idx = self.rooms.indexOf(room);
if (idx >= 0) {
self.rooms.splice(idx, 1);
}
fn && fn(null);
});
return this;
@@ -387,7 +391,12 @@ Socket.prototype.ondisconnect = function(){
*/
Socket.prototype.onerror = function(err){
this.emit('error', err);
if (this.listeners('error').length) {
this.emit('error', err);
} else {
console.error('Missing error handler on `socket`.');
console.error(err.stack);
}
};
/**
@@ -439,3 +448,17 @@ Socket.prototype.disconnect = function(close){
}
return this;
};
/**
* Sets the compress flag.
*
* @param {Boolean} if `true`, compresses the sending data
* @return {Socket} self
* @api public
*/
Socket.prototype.compress = function(compress){
this.flags = this.flags || {};
this.flags.compress = compress;
return this;
};

View File

@@ -1,6 +1,6 @@
{
"name": "socket.io",
"version": "1.2.1",
"version": "1.3.2",
"description": "node.js realtime framework server",
"keywords": [
"realtime",
@@ -16,15 +16,15 @@
"url": "git://github.com/Automattic/socket.io"
},
"scripts": {
"test": "make test"
"test": "mocha --reporter dot --slow 200ms --bail"
},
"dependencies": {
"engine.io": "1.4.3",
"socket.io-parser": "2.2.2",
"socket.io-client": "1.2.1",
"socket.io-adapter": "0.3.1",
"has-binary-data": "0.1.3",
"debug": "0.7.4"
"engine.io": "automattic/engine.io#ddc64a",
"socket.io-parser": "2.2.3",
"socket.io-client": "automattic/socket.io-client#210d65",
"socket.io-adapter": "automattic/socket.io-adapter#ae79d8",
"has-binary": "0.1.6",
"debug": "2.1.0"
},
"devDependencies": {
"mocha": "1.16.2",

View File

@@ -15,7 +15,7 @@ function client(srv, nsp, opts){
}
var addr = srv.address();
if (!addr) addr = srv.listen().address();
var url = 'ws://' + addr.address + ':' + addr.port + (nsp || '');
var url = 'ws://localhost:' + addr.port + (nsp || '');
return ioc(url, opts);
}
@@ -127,7 +127,7 @@ describe('socket.io', function(){
expect(s.handshake.time.split(' ').length > 0); // Is "multipart" string representation
// Address, xdomain, secure, issued and url set
expect(s.handshake.address).to.be('127.0.0.1');
expect(s.handshake.address).to.contain('127.0.0.1');
expect(s.handshake.xdomain).to.be.a('boolean');
expect(s.handshake.secure).to.be.a('boolean');
expect(s.handshake.issued).to.be.a('number');
@@ -302,6 +302,32 @@ describe('socket.io', function(){
done();
});
});
it('should allow request when origin defined as function and no origin is supplied', function(done) {
var sockets = io({ origins: function(origin,callback){
if (origin == '*') {
return callback(null, true);
}
return callback(null, false);
} }).listen('54021');
request.get('http://localhost:54021/socket.io/default/')
.query({ transport: 'polling' })
.end(function (err, res) {
expect(res.status).to.be(200);
done();
});
});
it('should default to port 443 when protocol is https', function(done) {
var sockets = io({ origins: 'https://foo.example:443' }).listen('54036');
request.get('http://localhost:54036/socket.io/default/')
.set('origin', 'https://foo.example')
.query({ transport: 'polling' })
.end(function (err, res) {
expect(res.status).to.be(200);
done();
});
});
});
describe('close', function(){
@@ -447,7 +473,7 @@ describe('socket.io', function(){
var c1 = client(srv, '/');
var c2 = client(srv, '/abc');
});
it('should be equivalent for "" and "/" on client', function(done){
var srv = http();
var sio = io(srv);
@@ -456,7 +482,7 @@ describe('socket.io', function(){
});
var c1 = client(srv, '');
});
it('should work with `of` and many sockets', function(done){
var srv = http();
var sio = io(srv);
@@ -571,6 +597,211 @@ describe('socket.io', function(){
});
});
});
it('should not reuse same-namespace connections', function(done){
var srv = http();
var sio = io(srv);
var connections = 0;
srv.listen(function() {
var clientSocket1 = client(srv);
var clientSocket2 = client(srv);
sio.on('connection', function() {
connections++;
if(connections === 2) {
done();
}
});
});
});
it('should find all clients in a namespace', function(done){
var srv = http();
var sio = io(srv);
var chatSids = [];
var otherSid = null;
srv.listen(function(){
var c1 = client(srv, '/chat');
var c2 = client(srv, '/chat', {forceNew: true});
var c3 = client(srv, '/other', {forceNew: true});
var total = 3;
sio.of('/chat').on('connection', function(socket){
chatSids.push(socket.id);
--total || getClients();
});
sio.of('/other').on('connection', function(socket){
otherSid = socket.id;
--total || getClients();
});
});
function getClients() {
sio.of('/chat').clients(function(error, sids) {
expect(error).to.be.undefined;
expect(sids).to.contain(chatSids[0]);
expect(sids).to.contain(chatSids[1]);
expect(sids).to.not.contain(otherSid);
done();
});
}
});
it('should find all clients in a namespace room', function(done){
var srv = http();
var sio = io(srv);
var chatFooSid = null;
var chatBarSid = null;
var otherSid = null;
srv.listen(function(){
var c1 = client(srv, '/chat');
var c2 = client(srv, '/chat', {forceNew: true});
var c3 = client(srv, '/other', {forceNew: true});
var chatIndex = 0;
var total = 3;
sio.of('/chat').on('connection', function(socket){
if (chatIndex++) {
socket.join('foo', function() {
chatFooSid = socket.id;
--total || getClients();
});
} else {
socket.join('bar', function() {
chatBarSid = socket.id;
--total || getClients();
});
}
});
sio.of('/other').on('connection', function(socket){
socket.join('foo', function() {
otherSid = socket.id;
--total || getClients();
});
});
});
function getClients() {
sio.of('/chat').in('foo').clients(function(error, sids) {
expect(error).to.be.undefined;
expect(sids).to.contain(chatFooSid);
expect(sids).to.not.contain(chatBarSid);
expect(sids).to.not.contain(otherSid);
done();
});
}
});
it('should find all clients across namespace rooms', function(done){
var srv = http();
var sio = io(srv);
var chatFooSid = null;
var chatBarSid = null;
var otherSid = null;
srv.listen(function(){
var c1 = client(srv, '/chat');
var c2 = client(srv, '/chat', {forceNew: true});
var c3 = client(srv, '/other', {forceNew: true});
var chatIndex = 0;
var total = 3;
sio.of('/chat').on('connection', function(socket){
if (chatIndex++) {
socket.join('foo', function() {
chatFooSid = socket.id;
--total || getClients();
});
} else {
socket.join('bar', function() {
chatBarSid = socket.id;
--total || getClients();
});
}
});
sio.of('/other').on('connection', function(socket){
socket.join('foo', function() {
otherSid = socket.id;
--total || getClients();
});
});
});
function getClients() {
sio.of('/chat').clients(function(error, sids) {
expect(error).to.be.undefined;
expect(sids).to.contain(chatFooSid);
expect(sids).to.contain(chatBarSid);
expect(sids).to.not.contain(otherSid);
done();
});
}
});
it('should allow connections to dynamic namespaces', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var namespace = '/dynamic';
var dynamic = client(srv,namespace);
sio.useNamespaceValidator(function(nsp, next) {
expect(nsp).to.be(namespace);
next(null, true);
});
dynamic.on('error', function(err) {
expect().fail();
});
dynamic.on('connect', function() {
expect(sio.nsps[namespace]).to.be.a(Namespace);
expect(sio.nsps[namespace].sockets.length).to.be(1);
done();
});
});
});
it('should not allow connections to dynamic namespaces if not supported', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var namespace = '/dynamic';
sio.useNamespaceValidator(function(nsp, next) {
expect(nsp).to.be(namespace);
next(null, false);
});
sio.on('connect', function(socket) {
if (socket.nsp.name === namespace) {
expect().fail();
}
});
var dynamic = client(srv,namespace);
dynamic.on('connect', function(){
expect().fail();
});
dynamic.on('error', function(err) {
expect(err).to.be("Invalid namespace");
done();
});
});
});
it('should not allow connections to dynamic namespaces if there is an error', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var namespace = '/dynamic';
sio.useNamespaceValidator(function(nsp, next) {
expect(nsp).to.be(namespace);
next(new Error(), true);
});
sio.on('connect', function(socket) {
if (socket.nsp.name === namespace) {
expect().fail();
}
});
var dynamic = client(srv,namespace);
dynamic.on('connect', function(){
expect().fail();
});
dynamic.on('error', function(err) {
expect(err).to.be("Invalid namespace");
done();
});
});
});
});
describe('socket', function(){
@@ -800,6 +1031,208 @@ describe('socket.io', function(){
});
});
it('should not emit volatile event after regular event (polling)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['polling'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
s.emit('ev', 'data');
s.volatile.emit('ev', 'data');
});
var socket = client(srv, { transports: ['polling'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(1);
done();
}, 200);
});
it('should not emit volatile event after regular event (ws)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['websocket'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
s.emit('ev', 'data');
s.volatile.emit('ev', 'data');
});
var socket = client(srv, { transports: ['websocket'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(1);
done();
}, 200);
});
it('should emit volatile event (polling)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['polling'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
// Wait to make sure there are no packets being sent for opening the connection
setTimeout(function() {
s.volatile.emit('ev', 'data');
}, 20);
});
var socket = client(srv, { transports: ['polling'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(1);
done();
}, 200);
});
it('should emit volatile event (ws)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['websocket'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
// Wait to make sure there are no packets being sent for opening the connection
setTimeout(function() {
s.volatile.emit('ev', 'data');
}, 20);
});
var socket = client(srv, { transports: ['websocket'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(1);
done();
}, 200);
});
it('should emit only one consecutive volatile event (polling)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['polling'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
// Wait to make sure there are no packets being sent for opening the connection
setTimeout(function() {
s.volatile.emit('ev', 'data');
s.volatile.emit('ev', 'data');
}, 20);
});
var socket = client(srv, { transports: ['polling'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(1);
done();
}, 200);
});
it('should emit only one consecutive volatile event (ws)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['websocket'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
// Wait to make sure there are no packets being sent for opening the connection
setTimeout(function() {
s.volatile.emit('ev', 'data');
s.volatile.emit('ev', 'data');
}, 20);
});
var socket = client(srv, { transports: ['websocket'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(1);
done();
}, 200);
});
it('should emit regular events after trying a failed volatile event (polling)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['polling'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
// Wait to make sure there are no packets being sent for opening the connection
setTimeout(function() {
s.emit('ev', 'data');
s.volatile.emit('ev', 'data');
s.emit('ev', 'data');
}, 20);
});
var socket = client(srv, { transports: ['polling'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(2);
done();
}, 200);
});
it('should emit regular events after trying a failed volatile event (ws)', function(done) {
var srv = http();
var sio = io(srv, { transports: ['websocket'] });
var counter = 0;
srv.listen(function(){
sio.on('connection', function(s){
// Wait to make sure there are no packets being sent for opening the connection
setTimeout(function() {
s.emit('ev', 'data');
s.volatile.emit('ev', 'data');
s.emit('ev', 'data');
}, 20);
});
var socket = client(srv, { transports: ['websocket'] });
socket.on('ev', function() {
counter++;
});
});
setTimeout(function() {
expect(counter).to.be(2);
done();
}, 200);
});
it('should emit message events through `send`', function(done){
var srv = http();
var sio = io(srv);
@@ -1022,7 +1455,7 @@ describe('socket.io', function(){
var sio = io(srv);
srv.listen(function() {
var addr = srv.listen().address();
var url = 'ws://' + addr.address + ':' + addr.port + '?key1=1&key2=2';
var url = 'ws://localhost:' + addr.port + '?key1=1&key2=2';
var socket = ioc(url);
sio.on('connection', function(s) {
var parsed = require('url').parse(s.request.url);
@@ -1035,7 +1468,7 @@ describe('socket.io', function(){
});
it('should handle very large json', function(done){
this.timeout();
this.timeout(30000);
var srv = http();
var sio = io(srv);
var received = 0;
@@ -1062,6 +1495,8 @@ describe('socket.io', function(){
});
it('should handle very large binary data', function(done){
this.timeout(10000);
var srv = http();
var sio = io(srv);
var received = 0;
@@ -1086,6 +1521,96 @@ describe('socket.io', function(){
});
});
});
it('should be able to emit after server close and restart', function(done){
var srv = http();
var sio = io(srv);
sio.on('connection', function(socket){
socket.on('ev', function(data){
expect(data).to.be('payload');
done();
});
});
srv.listen(function(){
var port = srv.address().port;
var clientSocket = client(srv, { reconnectionAttempts: 10, reconnectionDelay: 100 });
clientSocket.once('connect', function(){
srv.close(function(){
srv.listen(port, function(){
clientSocket.on('reconnect', function(){
clientSocket.emit('ev', 'payload');
});
});
});
});
});
});
it('should enable compression by default', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.conn.once('packetCreate', function(packet) {
expect(packet.options.compress).to.be(true);
done();
});
s.emit('woot', 'hi');
});
});
});
it('should disable compression', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.conn.once('packetCreate', function(packet) {
expect(packet.options.compress).to.be(false);
done();
});
s.compress(false).emit('woot', 'hi');
});
});
});
it('should error with raw binary and warn', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.conn.on('upgrade', function(){
console.log('\033[96mNote: warning expected and normal in test.\033[39m');
socket.io.engine.write('5woooot');
setTimeout(function(){
done();
}, 100);
});
});
});
});
it('should not crash with raw binary', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.once('error', function(err){
expect(err.message).to.match(/Illegal attachments/);
done();
});
s.conn.on('upgrade', function(){
socket.io.engine.write('5woooot');
});
});
});
});
});
describe('messaging many', function(){
@@ -1385,6 +1910,29 @@ describe('socket.io', function(){
});
});
});
it('should properly cleanup left rooms', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.join('a', function(){
expect(s.rooms).to.eql([s.id, 'a']);
s.join('b', function(){
expect(s.rooms).to.eql([s.id, 'a', 'b']);
s.leave('unknown', function(){
expect(s.rooms).to.eql([s.id, 'a', 'b']);
s.leaveAll();
expect(s.rooms).to.eql([]);
done();
});
});
});
});
});
});
});
describe('middleware', function(done){