Compare commits

...

70 Commits
1.1.0 ... 1.3.4

Author SHA1 Message Date
Guillermo Rauch
1b2d902f33 Release 1.3.4 2015-02-14 10:57:50 -08:00
Guillermo Rauch
9536437907 package: bump socket.io-client 2015-02-14 10:55:15 -08:00
Guillermo Rauch
29974ac777 Release 1.3.3 2015-02-03 17:27:51 -08:00
Guillermo Rauch
ef23c74bea remove compression tests 2015-02-03 17:07:43 -08:00
Guillermo Rauch
49423d70bc added failing tests 2015-02-03 17:04:33 -08:00
Guillermo Rauch
715c7f99b4 socket: warn node_redis-style about missing error 2015-02-03 17:04:06 -08:00
Guillermo Rauch
f2ea965c6b package: bump parser 2015-02-03 16:32:48 -08: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
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
Tony Kovanen
d9996f0470 Added tests for volatile 2014-12-08 11:19:49 +02:00
Guillermo Rauch
24d06d76dd Release 1.2.1 2014-11-21 04:59:14 +01:00
Guillermo Rauch
4e4bbf918e fix protocol violations and improve error handling (fixes #1880) 2014-11-21 04:16:37 +01:00
Guillermo Rauch
b49f5c82f2 syntax 2014-11-21 04:15:42 +01:00
Guillermo Rauch
5bd67195de Merge branch 'master' of github.com:Automattic/socket.io 2014-11-21 02:57:35 +01:00
Guillermo Rauch
73fe547956 package: bump engine.io for websocket leak fix 2014-11-21 02:56:28 +01:00
Guillermo Rauch
973e6cc982 Merge pull request #1813 from ogwiz2/patch-1
Made comments more consistent
2014-11-12 10:04:09 +01:00
Guillermo Rauch
4a0091b25a Release 1.2.0 2014-10-27 15:54:57 -07:00
Guillermo Rauch
a1bf85b57f package: bump socket.io-parser 2014-10-27 11:52:09 -07:00
Guillermo Rauch
62ad812ee5 package: use latest socket.io-client 2014-10-27 11:51:03 -07:00
Guillermo Rauch
9ae8ed6929 package: bump adapter 2014-10-27 11:46:44 -07:00
Guillermo Rauch
4a11c16cc2 package: bump socket.io-client 2014-10-27 11:09:44 -07:00
Guillermo Rauch
b88a758857 package: bump engine.io 2014-10-27 07:47:51 -07:00
Guillermo Rauch
3352d1d726 Merge branch 'master' of github.com:Automattic/socket.io 2014-10-13 08:52:46 -07:00
Guillermo Rauch
e68557fab6 downloads badge 2014-10-13 08:52:36 -07:00
Alex Jeng
136fe960b7 Made comments more consistent 2014-10-05 22:10:13 -07:00
Tony Kovanen
da358084f0 Merge pull request #1773 from AjayMT/master
Fix usage example for Server#close.
2014-09-26 01:13:18 +03:00
Tony Kovanen
1b4f6a5324 Merge pull request #1777 from akamensky/dynamic-cors
Dynamic cors
2014-09-22 13:38:14 -06:00
Tony Kovanen
99346eddc5 Merge pull request #1788 from jamesanthonyferguson/patch-1
Change grammar issues in the chat/README.md
2014-09-22 12:42:59 -06:00
Tony Kovanen
051ffa0440 Merge pull request #1626 from thanpolas/patch-1
http.Server is a constructor
2014-09-22 12:42:10 -06:00
Tony Kovanen
fa4fa3365a Merge pull request #1690 from rase-/fix/resource-option-bc
Fix resource option BC with the set function
2014-09-22 12:27:35 -06:00
Tony Kovanen
bafa184c6b Merge pull request #1792 from rase-/add/autopruning-test
Add room autopruning test
2014-09-22 12:25:23 -06:00
Tony Kovanen
e3149d5833 Bump adapter version. 2014-09-20 11:12:58 -06:00
Tony Kovanen
ae9d5277a9 Add test to check that empty rooms are autopruned. 2014-09-20 11:12:26 -06:00
James Ferguson
2e440722a6 Change grammar issues in the chat/README.md
Added a few periods and commas which were missing. Also added the word each to clarify how multiple users each enter a unique username.
Very minor edit. No source code changed.
2014-09-18 21:41:28 -07:00
Tony Kovanen
3fe6d4e8ec Merge pull request #1784 from matthewcanty/patch-1
Change "there're" to "there are"
2014-09-17 07:41:46 -06:00
Matthew Canty
3b93c1c562 Change "there're" to "there are"
Not a commonly used contraction and looks a bit odd.
2014-09-17 14:01:01 +01:00
akamensky
e89f82a9b0 Added Server#origins(v:Function) description for dynamic CORS 2014-09-12 11:32:20 +08:00
akamensky
1b90ae2587 Added test coverage for Server#origins(function) for dynamic CORS 2014-09-12 11:13:51 +08:00
akamensky
9658e32e7a Added optional Server#origins(function) for dynamic CORS 2014-09-12 10:53:14 +08:00
Tony Kovanen
99d76664aa Merge pull request #1766 from BrianGeppert/master
package: fix main file for example application 'chat'.
2014-09-11 18:05:58 +03:00
Ajay MT
23eefa527a Fix usage example for Server#close. 2014-09-10 18:11:56 +05:30
Brian Geppert
b49b35b26f package: fix main file for example application 'chat'. 2014-09-04 12:10:43 -05:00
Tony Kovanen
b4954d767a Take the instance stored path value into account when attaching to eio if no option specified to attach directly 2014-07-23 19:58:44 +03:00
Thanasis Polychronakis
63e197083b update README http ctor to createServer() 2014-07-01 10:41:53 +03:00
Thanasis Polychronakis
55fb100fc0 http.Server is a constructor
...and thus requires the `new` keyword
2014-06-14 13:52:36 +03:00
11 changed files with 545 additions and 40 deletions

View File

@@ -1,4 +1,61 @@
1.3.4 / 2015-02-14
==================
* package: bump `socket.io-client`
1.3.3 / 2015-02-03
==================
* socket: warn node_redis-style about missing `error`
* package: bump parser to better handle bad binary packets
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
==================
* fix protocol violations and improve error handling (GH-1880)
* package: bump `engine.io` for websocket leak fix [3rd-Eden]
* style tweaks
1.2.0 / 2014-10-27
==================
* package: bump `engine.io`
* downloads badge
* add test to check that empty rooms are autopruned
* added Server#origins(v:Function) description for dynamic CORS
* added test coverage for Server#origins(function) for dynamic CORS
* added optional Server#origins(function) for dynamic CORS
* fix usage example for Server#close
* package: fix main file for example application 'chat'
* package: bump `socket.io-parser`
* update README http ctor to createServer()
* bump adapter with a lot of fixes for room bookkeeping
1.1.0 / 2014-09-04
==================

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,7 +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)
![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
@@ -10,7 +11,7 @@ The following example attaches socket.io to a plain Node.JS
HTTP server listening on port `3000`.
```js
var server = require('http').Server();
var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(socket){
socket.on('event', function(data){});
@@ -36,7 +37,7 @@ function.
```js
var app = require('express')();
var server = require('http').Server(app);
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){ /* … */ });
server.listen(3000);
@@ -49,7 +50,7 @@ handler function, but only by calling the `callback` method.
```js
var app = require('koa')();
var server = require('http').Server(app.callback());
var server = require('http').createServer(app.callback());
var io = require('socket.io')(server);
io.on('connection', function(){ /* … */ });
server.listen(3000);
@@ -136,6 +137,15 @@ server.listen(3000);
If no arguments are supplied this method returns the current value.
### Server#origins(v:Function):Server
Sets the allowed origins as dynamic function. Function takes two arguments `origin:String` and `callback(error, success)`, where `success` is a boolean value indicating whether origin is allowed or not.
__Potential drawbacks__:
* in some situations, when it is not possible to determine `origin` it may have value of `*`
* As this function will be executed for every request, it is advised to make this function work as fast as possible
* If `socket.io` is used together with `Express`, the CORS headers will be affected only for `socket.io` requests. For Express can use [cors](https://github.com/troygoode/node-cors/)
### Server#sockets:Namespace
@@ -190,17 +200,17 @@ server.listen(3000);
Closes socket server
```js
var io = require('socket.io');
var Server = require('socket.io');
var PORT = 3030;
var server = require('http').Server();
io(PORT);
var io = Server(PORT);
io.close(); // Close current server
server.listen(PORT); // PORT is free to use
io(server);
io = Server(server);
```
### Server#use
@@ -264,7 +274,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

View File

@@ -13,13 +13,13 @@ $ npm install
$ node .
```
And point your browser to `http://localhost:3000`. Optionally specify
And point your browser to `http://localhost:3000`. Optionally, specify
a port by supplying the `PORT` env variable.
## Features
- Multiple users can join a chat room by entering a unique username
- Multiple users can join a chat room by each entering a unique username
on website load.
- Users can type chat messages to the chat room
- Users can type chat messages to the chat room.
- A notification is sent to all users when a user joins or leaves
the chatroom
the chatroom.

View File

@@ -2,7 +2,7 @@
"name": "socket.io-chat",
"version": "0.0.0",
"description": "A simple chat client using socket.io",
"main": "app.js",
"main": "index.js",
"author": "Grant Timmerman",
"private": true,
"license": "BSD",

View File

@@ -30,7 +30,7 @@ $(function() {
if (data.numUsers === 1) {
message += "there's 1 participant";
} else {
message += "there're " + data.numUsers + " participants";
message += "there are " + data.numUsers + " participants";
}
log(message);
}

View File

@@ -42,9 +42,12 @@ function Client(server, conn){
Client.prototype.setup = function(){
this.onclose = this.onclose.bind(this);
this.ondata = this.ondata.bind(this);
this.onerror = this.onerror.bind(this);
this.ondecoded = this.ondecoded.bind(this);
this.decoder.on('decoded', this.ondecoded);
this.conn.on('data', this.ondata);
this.conn.on('error', this.onerror);
this.conn.on('close', this.onclose);
};
@@ -167,7 +170,12 @@ Client.prototype.packet = function(packet, preEncoded, volatile){
*/
Client.prototype.ondata = function(data){
this.decoder.add(data);
// try/catch is needed for protocol violations (GH-1880)
try {
this.decoder.add(data);
} catch(e) {
this.onerror(e);
}
};
/**
@@ -189,6 +197,20 @@ Client.prototype.ondecoded = function(packet) {
}
};
/**
* Handles an error.
*
* @param {Objcet} error object
* @api private
*/
Client.prototype.onerror = function(err){
this.sockets.forEach(function(socket){
socket.onerror(err);
});
this.onclose('client error');
};
/**
* Called upon transport close.
*
@@ -219,6 +241,7 @@ Client.prototype.onclose = function(reason){
Client.prototype.destroy = function(){
this.conn.removeListener('data', this.ondata);
this.conn.removeListener('error', this.onerror);
this.conn.removeListener('close', this.onclose);
this.decoder.removeListener('decoded', this.ondecoded);
};

View File

@@ -62,8 +62,9 @@ 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 {
@@ -193,7 +194,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);
}
@@ -216,7 +217,7 @@ Server.prototype.attach = function(srv, opts){
// set engine.io path to `/socket.io`
opts = opts || {};
opts.path = opts.path || '/socket.io';
opts.path = opts.path || this.path();
// set origins verification
opts.allowRequest = this.checkRequest.bind(this);

View File

@@ -242,7 +242,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;
@@ -380,10 +383,26 @@ Socket.prototype.ondisconnect = function(){
this.onclose('client namespace disconnect');
};
/**
* Handles a client error.
*
* @api private
*/
Socket.prototype.onerror = function(err){
if (this.listeners('error').length) {
this.emit('error', err);
} else {
console.error('Missing error handler on `socket`.');
console.error(err.stack);
}
};
/**
* Called upon closing. Called by `Client`.
*
* @param {String} reason
* @param {Error} optional error object
* @api private
*/

View File

@@ -1,6 +1,6 @@
{
"name": "socket.io",
"version": "1.1.0",
"version": "1.3.4",
"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.0",
"socket.io-parser": "2.2.1",
"socket.io-client": "1.1.0",
"socket.io-adapter": "0.2.0",
"engine.io": "1.5.1",
"socket.io-parser": "2.2.3",
"socket.io-client": "1.3.4",
"socket.io-adapter": "0.3.1",
"has-binary-data": "0.1.3",
"debug": "0.7.4"
"debug": "2.1.0"
},
"devDependencies": {
"mocha": "1.16.2",

View File

@@ -7,7 +7,7 @@ var ioc = require('socket.io-client');
var request = require('supertest');
var expect = require('expect.js');
// creates a socket.io client for the given server
// Creates a socket.io client for the given server
function client(srv, nsp, opts){
if ('object' == typeof nsp) {
opts = nsp;
@@ -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);
}
@@ -51,10 +51,21 @@ describe('socket.io', function(){
expect(srv.eio.maxHttpBufferSize).to.eql(10);
});
it('should be able to set path with setting resource', function() {
var srv = io(http());
srv.set('resource', '/random');
expect(srv.path()).to.be('/random');
it('should be able to set path with setting resource', function(done) {
var eio = io();
var srv = http();
eio.set('resource', '/random');
eio.attach(srv);
// Check that the server is accessible through the specified path
request(srv)
.get('/random/socket.io.js')
.buffer(true)
.end(function(err, res){
if (err) return done(err);
done();
});
});
it('should be able to set origins to engine.io', function() {
@@ -116,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');
@@ -259,12 +270,59 @@ describe('socket.io', function(){
done();
});
});
it('should allow request when origin defined as function and same is supplied', function(done) {
var sockets = io({ origins: function(origin,callback){
if (origin == 'http://foo.example') {
return callback(null, true);
}
return callback(null, false);
} }).listen('54016');
request.get('http://localhost:54016/socket.io/default/')
.set('origin', 'http://foo.example')
.query({ transport: 'polling' })
.end(function (err, res) {
expect(res.status).to.be(200);
done();
});
});
it('should allow request when origin defined as function and different is supplied', function(done) {
var sockets = io({ origins: function(origin,callback){
if (origin == 'http://foo.example') {
return callback(null, true);
}
return callback(null, false);
} }).listen('54017');
request.get('http://localhost:54017/socket.io/default/')
.set('origin', 'http://herp.derp')
.query({ transport: 'polling' })
.end(function (err, res) {
expect(res.status).to.be(400);
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();
});
});
});
describe('close', function(){
it('should be able to close sio sending a srv', function(){
var PORT = 54016;
var PORT = 54018;
var srv = http().listen(PORT);
var sio = io(srv);
var net = require('net');
@@ -292,7 +350,7 @@ describe('socket.io', function(){
});
it('should be able to close sio sending a port', function(){
var PORT = 54017;
var PORT = 54019;
var sio = io(PORT);
var net = require('net');
var server = net.createServer();
@@ -404,7 +462,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);
@@ -413,7 +471,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);
@@ -596,6 +654,39 @@ describe('socket.io', function(){
});
});
it('should error with null messages', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.on('message', function(a){
expect(a).to.be(null);
done();
});
socket.send(null);
});
});
});
it('should handle transport null messages', function(done){
var srv = http();
var sio = io(srv);
srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.on('error', function(err){
expect(err).to.be.an(Error);
s.on('disconnect', function(reason){
expect(reason).to.be('client error');
done();
});
});
s.client.ondata(null);
});
});
});
it('should emit events', function(done){
var srv = http();
var sio = io(srv);
@@ -724,6 +815,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);
@@ -946,7 +1239,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);
@@ -959,7 +1252,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;
@@ -1010,6 +1303,66 @@ 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 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(){
@@ -1291,6 +1644,48 @@ describe('socket.io', function(){
});
});
});
it('deletes empty 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.nsp.adapter.rooms).to.have.key('a');
s.leave('a', function(){
expect(s.nsp.adapter.rooms).to.not.have.key('a');
done();
});
});
});
});
});
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){