mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-11 16:08:24 -05:00
Compare commits
64 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16483375a7 | ||
|
|
ec305bd8df | ||
|
|
af07a1c649 | ||
|
|
5863903d44 | ||
|
|
f56d4acce6 | ||
|
|
8ea37b7351 | ||
|
|
b3cb18d910 | ||
|
|
0e41561d56 | ||
|
|
83709e9487 | ||
|
|
6f4051aaa7 | ||
|
|
87bf6910b5 | ||
|
|
8946b4ed4c | ||
|
|
a40068b5f3 | ||
|
|
9e29ec9b2b | ||
|
|
624e7cb14f | ||
|
|
f6eb53f5e1 | ||
|
|
9c80317574 | ||
|
|
d163d891ef | ||
|
|
54726105cb | ||
|
|
3d9e52cf93 | ||
|
|
ad74f2ff88 | ||
|
|
cf1c1273b3 | ||
|
|
1f43c4abb5 | ||
|
|
d6e6e8a2f8 | ||
|
|
fbd36b613d | ||
|
|
480b1a05bd | ||
|
|
ef729b72b4 | ||
|
|
968e94e42b | ||
|
|
c18ed5fd4d | ||
|
|
1f7da938bd | ||
|
|
4b89bce182 | ||
|
|
8c19eef07a | ||
|
|
662b30669b | ||
|
|
001373ee17 | ||
|
|
246f3bb5c3 | ||
|
|
68d06ec94c | ||
|
|
a1feca1bd3 | ||
|
|
968105a239 | ||
|
|
2e8e26613a | ||
|
|
fbdb94d146 | ||
|
|
55572122f3 | ||
|
|
a1170a3aa6 | ||
|
|
0954301d7e | ||
|
|
328a9df8eb | ||
|
|
d99e30fca7 | ||
|
|
561dd6fd79 | ||
|
|
e9e2a91cea | ||
|
|
8a3a111a7f | ||
|
|
bd87e4dc35 | ||
|
|
71c253e992 | ||
|
|
a5cf4f57a0 | ||
|
|
a079cbc7f9 | ||
|
|
14a9fdb64f | ||
|
|
6159df3937 | ||
|
|
6f7bab5dfd | ||
|
|
7d2b44e176 | ||
|
|
0b5fdf995a | ||
|
|
a66bea5b33 | ||
|
|
a1a88aaaaf | ||
|
|
3f817c3a18 | ||
|
|
99bbd74d14 | ||
|
|
398511ddee | ||
|
|
fdf7937815 | ||
|
|
5812ddf2b0 |
@@ -1,3 +1,4 @@
|
||||
support
|
||||
test
|
||||
examples
|
||||
.gitignore
|
||||
|
||||
@@ -4,6 +4,9 @@ node_js:
|
||||
- "0.10"
|
||||
- "0.11"
|
||||
|
||||
git:
|
||||
depth: 1
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
allow_failures:
|
||||
|
||||
44
History.md
44
History.md
@@ -1,4 +1,48 @@
|
||||
|
||||
1.1.0 / 2014-09-04
|
||||
==================
|
||||
|
||||
* examples: minor fix of escaping
|
||||
* testing for equivalence of namespaces starting with / or without
|
||||
* update index.js
|
||||
* added relevant tests
|
||||
* take "" and "/" as equivalent namespaces on server
|
||||
* use svg instead of png to get better image quality in readme
|
||||
* make CI build faster
|
||||
* fix splice arguments and `socket.rooms` value update in `socket.leaveAll`.
|
||||
* client cannot connect to non-existing namespaces
|
||||
* bump engine.io version to get the cached IP address
|
||||
* fixed handshake object address property and made the test case more strict.
|
||||
* package: bump `engine.io`
|
||||
* fixed the failing test where server crashes on disconnect involving connectBuffer
|
||||
* npmignore: ignore `.gitignore` (fixes #1607)
|
||||
* test: added failing case for `socket.disconnect` and nsps
|
||||
* fix repo in package.json
|
||||
* improve Close documentation
|
||||
* use ephemeral ports
|
||||
* fix: We should use the standard http protocol to handler the etag header.
|
||||
* override default browser font-family for inputs
|
||||
* update has-binary-data to 1.0.3
|
||||
* add close specs
|
||||
* add ability to stop the http server even if not created inside socket.io
|
||||
* make sure server gets close
|
||||
* Add test case for checking that reconnect_failed is fired only once upon failure
|
||||
* package: bump `socket.io-parser` for `component-emitter` dep fix
|
||||
|
||||
1.0.6 / 2014-06-19
|
||||
==================
|
||||
|
||||
* package: bump `socket.io-client`
|
||||
|
||||
1.0.5 / 2014-06-16
|
||||
==================
|
||||
|
||||
* package: bump `engine.io` to fix jsonp `\n` bug and CORS warnings
|
||||
* index: fix typo [yanatan16]
|
||||
* add `removeListener` to blacklisted events
|
||||
* examples: clearer instructions to install chat example
|
||||
* index: fix namespace `connectBuffer` issue
|
||||
|
||||
1.0.4 / 2014-06-02
|
||||
==================
|
||||
|
||||
|
||||
28
Readme.md
28
Readme.md
@@ -1,8 +1,8 @@
|
||||
|
||||
# socket.io
|
||||
|
||||
[](http://travis-ci.org/Automattic/socket.io)
|
||||
[](http://badge.fury.io/js/socket.io)
|
||||
[](http://travis-ci.org/Automattic/socket.io)
|
||||
[](http://badge.fury.io/js/socket.io)
|
||||
|
||||
## How to use
|
||||
|
||||
@@ -157,7 +157,7 @@ server.listen(3000);
|
||||
|
||||
### Server#bind(srv:engine#Server):Server
|
||||
|
||||
Advanced use only. Binds the server to a specific engine.io `Server`
|
||||
Advanced use only. Binds the server to a specific engine.io `Server`
|
||||
(or compatible API) instance.
|
||||
|
||||
### Server#onconnection(socket:engine#Socket):Server
|
||||
@@ -167,14 +167,14 @@ server.listen(3000);
|
||||
|
||||
### Server#of(nsp:String):Namespace
|
||||
|
||||
Initializes and retrieves the given `Namespace` by its pathname
|
||||
Initializes and retrieves the given `Namespace` by its pathname
|
||||
identifier `nsp`.
|
||||
|
||||
If the namespace was already initialized it returns it right away.
|
||||
|
||||
### Server#emit
|
||||
|
||||
Emits an event to all connected clients. The following two are
|
||||
Emits an event to all connected clients. The following two are
|
||||
equivalent:
|
||||
|
||||
```js
|
||||
@@ -185,6 +185,24 @@ server.listen(3000);
|
||||
|
||||
For other available methods, see `Namespace` below.
|
||||
|
||||
### Server#close
|
||||
|
||||
Closes socket server
|
||||
|
||||
```js
|
||||
var io = require('socket.io');
|
||||
var PORT = 3030;
|
||||
var server = require('http').Server();
|
||||
|
||||
io(PORT);
|
||||
|
||||
io.close(); // Close current server
|
||||
|
||||
server.listen(PORT); // PORT is free to use
|
||||
|
||||
io(server);
|
||||
```
|
||||
|
||||
### Server#use
|
||||
|
||||
See `Namespace#use` below.
|
||||
|
||||
@@ -6,6 +6,9 @@ A simple chat demo for socket.io
|
||||
## How to use
|
||||
|
||||
```
|
||||
$ cd socket.io
|
||||
$ npm install
|
||||
$ cd examples/chat
|
||||
$ npm install
|
||||
$ node .
|
||||
```
|
||||
|
||||
@@ -229,7 +229,7 @@ $(function() {
|
||||
socket.on('login', function (data) {
|
||||
connected = true;
|
||||
// Display the welcome message
|
||||
var message = "Welcome to Socket.IO Chat — ";
|
||||
var message = "Welcome to Socket.IO Chat – ";
|
||||
log(message, {
|
||||
prepend: true
|
||||
});
|
||||
@@ -263,4 +263,4 @@ $(function() {
|
||||
socket.on('stop typing', function (data) {
|
||||
removeChatTyping(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
}
|
||||
|
||||
html {
|
||||
font-weight: 300;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
html, input {
|
||||
font-family:
|
||||
"HelveticaNeue-Light",
|
||||
"Helvetica Neue Light",
|
||||
@@ -13,8 +18,6 @@ html {
|
||||
Arial,
|
||||
"Lucida Grande",
|
||||
sans-serif;
|
||||
font-weight: 300;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
html, body {
|
||||
@@ -144,4 +147,4 @@ ul {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,10 @@ 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);
|
||||
@@ -68,9 +72,9 @@ Client.prototype.connect = function(name){
|
||||
self.sockets.push(socket);
|
||||
self.nsps[nsp.name] = socket;
|
||||
|
||||
if ('/' == nsp.name) {
|
||||
if ('/' == nsp.name && self.connectBuffer.length > 0) {
|
||||
self.connectBuffer.forEach(self.connect, self);
|
||||
delete self.connectBuffer;
|
||||
self.connectBuffer = [];
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
33
lib/index.js
33
lib/index.js
@@ -159,7 +159,9 @@ Server.prototype.adapter = function(v){
|
||||
if (!arguments.length) return this._adapter;
|
||||
this._adapter = v;
|
||||
for (var i in this.nsps) {
|
||||
this.nsps[i].initAdapter();
|
||||
if (this.nsps.hasOwnProperty(i)) {
|
||||
this.nsps[i].initAdapter();
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
@@ -209,6 +211,7 @@ Server.prototype.attach = function(srv, opts){
|
||||
res.end();
|
||||
});
|
||||
srv.listen(port);
|
||||
|
||||
}
|
||||
|
||||
// set engine.io path to `/socket.io`
|
||||
@@ -224,6 +227,9 @@ Server.prototype.attach = function(srv, opts){
|
||||
// attach static file serving
|
||||
if (this._serveClient) this.attachServe(srv);
|
||||
|
||||
// Export http server
|
||||
this.httpServer = srv;
|
||||
|
||||
// bind to engine events
|
||||
this.bind(this.eio);
|
||||
|
||||
@@ -263,8 +269,9 @@ Server.prototype.attachServe = function(srv){
|
||||
*/
|
||||
|
||||
Server.prototype.serve = function(req, res){
|
||||
if (req.headers.etag) {
|
||||
if (clientVersion == req.headers.etag) {
|
||||
var etag = req.headers['if-none-match'];
|
||||
if (etag) {
|
||||
if (clientVersion == etag) {
|
||||
debug('serve client 304');
|
||||
res.writeHead(304);
|
||||
res.end();
|
||||
@@ -317,6 +324,8 @@ Server.prototype.onconnection = function(conn){
|
||||
*/
|
||||
|
||||
Server.prototype.of = function(name, fn){
|
||||
if (String(name)[0] !== '/') name = '/' + name;
|
||||
|
||||
if (!this.nsps[name]) {
|
||||
debug('initializing namespace %s', name);
|
||||
var nsp = new Namespace(this, name);
|
||||
@@ -326,6 +335,24 @@ Server.prototype.of = function(name, fn){
|
||||
return this.nsps[name];
|
||||
};
|
||||
|
||||
/**
|
||||
* Closes server connection
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Server.prototype.close = function(){
|
||||
this.nsps['/'].sockets.forEach(function(socket){
|
||||
socket.onclose();
|
||||
});
|
||||
|
||||
this.engine.close();
|
||||
|
||||
if(this.httpServer){
|
||||
this.httpServer.close();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Expose main namespace (/).
|
||||
*/
|
||||
|
||||
@@ -25,7 +25,8 @@ exports.events = [
|
||||
'error',
|
||||
'connect',
|
||||
'disconnect',
|
||||
'newListener'
|
||||
'newListener',
|
||||
'removeListener'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -107,7 +108,7 @@ Socket.prototype.buildHandshake = function(){
|
||||
return {
|
||||
headers: this.request.headers,
|
||||
time: (new Date) + '',
|
||||
address: this.request.connection.address(),
|
||||
address: this.conn.remoteAddress,
|
||||
xdomain: !!this.request.headers.origin,
|
||||
secure: !!this.request.connection.encrypted,
|
||||
issued: +(new Date),
|
||||
@@ -241,7 +242,7 @@ 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));
|
||||
self.rooms.splice(self.rooms.indexOf(room), 1);
|
||||
fn && fn(null);
|
||||
});
|
||||
return this;
|
||||
@@ -255,6 +256,7 @@ Socket.prototype.leave = function(room, fn){
|
||||
|
||||
Socket.prototype.leaveAll = function(){
|
||||
this.adapter.delAll(this.id);
|
||||
this.rooms = [];
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
12
package.json
12
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "socket.io",
|
||||
"version": "1.0.4",
|
||||
"version": "1.1.0",
|
||||
"description": "node.js realtime framework server",
|
||||
"keywords": [
|
||||
"realtime",
|
||||
@@ -13,17 +13,17 @@
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/LearnBoost/socket.io"
|
||||
"url": "git://github.com/Automattic/socket.io"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "make test"
|
||||
},
|
||||
"dependencies": {
|
||||
"engine.io": "1.2.2",
|
||||
"socket.io-parser": "2.2.0",
|
||||
"socket.io-client": "1.0.4",
|
||||
"engine.io": "1.4.0",
|
||||
"socket.io-parser": "2.2.1",
|
||||
"socket.io-client": "1.1.0",
|
||||
"socket.io-adapter": "0.2.0",
|
||||
"has-binary-data": "0.1.1",
|
||||
"has-binary-data": "0.1.3",
|
||||
"debug": "0.7.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -116,7 +116,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.not.be(undefined);
|
||||
expect(s.handshake.address).to.be('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');
|
||||
@@ -161,7 +161,7 @@ describe('socket.io', function(){
|
||||
io(srv);
|
||||
request(srv)
|
||||
.get('/socket.io/socket.io.js')
|
||||
.set('ETag', clientVersion)
|
||||
.set('If-None-Match', clientVersion)
|
||||
.end(function(err, res){
|
||||
if (err) return done(err);
|
||||
expect(res.statusCode).to.be(304);
|
||||
@@ -261,171 +261,309 @@ describe('socket.io', function(){
|
||||
});
|
||||
});
|
||||
|
||||
describe('close', function(){
|
||||
|
||||
it('should be able to close sio sending a srv', function(){
|
||||
var PORT = 54016;
|
||||
var srv = http().listen(PORT);
|
||||
var sio = io(srv);
|
||||
var net = require('net');
|
||||
var server = net.createServer();
|
||||
|
||||
var clientSocket = client(srv, { reconnection: false });
|
||||
|
||||
clientSocket.on('disconnect', function init() {
|
||||
expect(sio.nsps['/'].sockets.length).to.equal(0);
|
||||
server.listen(PORT);
|
||||
});
|
||||
|
||||
clientSocket.on('connect', function init() {
|
||||
expect(sio.nsps['/'].sockets.length).to.equal(1);
|
||||
sio.close();
|
||||
});
|
||||
|
||||
server.once('listening', function() {
|
||||
// PORT should be free
|
||||
server.close(function(error){
|
||||
expect(error).to.be(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should be able to close sio sending a port', function(){
|
||||
var PORT = 54017;
|
||||
var sio = io(PORT);
|
||||
var net = require('net');
|
||||
var server = net.createServer();
|
||||
|
||||
var clientSocket = ioc('ws://0.0.0.0:' + PORT);
|
||||
|
||||
clientSocket.on('disconnect', function init() {
|
||||
expect(sio.nsps['/'].sockets.length).to.equal(0);
|
||||
server.listen(PORT);
|
||||
});
|
||||
|
||||
clientSocket.on('connect', function init() {
|
||||
expect(sio.nsps['/'].sockets.length).to.equal(1);
|
||||
sio.close();
|
||||
});
|
||||
|
||||
server.once('listening', function() {
|
||||
// PORT should be free
|
||||
server.close(function(error){
|
||||
expect(error).to.be(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('namespaces', function(){
|
||||
var Socket = require('../lib/socket');
|
||||
var Namespace = require('../lib/namespace');
|
||||
|
||||
describe('default', function(){
|
||||
it('should be accessible through .sockets', function(){
|
||||
var sio = io();
|
||||
expect(sio.sockets).to.be.a(Namespace);
|
||||
});
|
||||
it('should be accessible through .sockets', function(){
|
||||
var sio = io();
|
||||
expect(sio.sockets).to.be.a(Namespace);
|
||||
});
|
||||
|
||||
it('should be aliased', function(){
|
||||
var sio = io();
|
||||
expect(sio.use).to.be.a('function');
|
||||
expect(sio.to).to.be.a('function');
|
||||
expect(sio['in']).to.be.a('function');
|
||||
expect(sio.emit).to.be.a('function');
|
||||
expect(sio.send).to.be.a('function');
|
||||
expect(sio.write).to.be.a('function');
|
||||
});
|
||||
it('should be aliased', function(){
|
||||
var sio = io();
|
||||
expect(sio.use).to.be.a('function');
|
||||
expect(sio.to).to.be.a('function');
|
||||
expect(sio['in']).to.be.a('function');
|
||||
expect(sio.emit).to.be.a('function');
|
||||
expect(sio.send).to.be.a('function');
|
||||
expect(sio.write).to.be.a('function');
|
||||
});
|
||||
|
||||
it('should automatically connect', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv);
|
||||
socket.on('connect', function(){
|
||||
done();
|
||||
});
|
||||
it('should automatically connect', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv);
|
||||
socket.on('connect', function(){
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should fire a `connection` event', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv);
|
||||
sio.on('connection', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
done();
|
||||
});
|
||||
it('should fire a `connection` event', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv);
|
||||
sio.on('connection', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should fire a `connect` event', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv);
|
||||
sio.on('connect', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
done();
|
||||
});
|
||||
it('should fire a `connect` event', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv);
|
||||
sio.on('connect', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with many sockets', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
chat.on('connect', function(){
|
||||
--total || done();
|
||||
});
|
||||
news.on('connect', function(){
|
||||
--total || done();
|
||||
});
|
||||
it('should work with many sockets', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
sio.of('/chat');
|
||||
sio.of('/news');
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
chat.on('connect', function(){
|
||||
--total || done();
|
||||
});
|
||||
news.on('connect', function(){
|
||||
--total || done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with `of` and many sockets', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
sio.of('/news').on('connection', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
sio.of('/news').on('connection', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
it('should be able to equivalently start with "" or "/" on server', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
var total = 2;
|
||||
sio.of('').on('connection', function(){
|
||||
--total || done();
|
||||
});
|
||||
sio.of('abc').on('connection', function(){
|
||||
--total || done();
|
||||
});
|
||||
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);
|
||||
sio.of('/').on('connection', function(){
|
||||
done();
|
||||
});
|
||||
var c1 = client(srv, '');
|
||||
});
|
||||
|
||||
it('should work with `of` and many sockets', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
sio.of('/news').on('connection', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
sio.of('/news').on('connection', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with `of` second param', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
sio.of('/news', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
sio.of('/news', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
it('should work with `of` second param', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
sio.of('/news', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
sio.of('/news', function(socket){
|
||||
expect(socket).to.be.a(Socket);
|
||||
--total || done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should disconnect upon transport disconnection', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
var totald = 2;
|
||||
var s;
|
||||
sio.of('/news', function(socket){
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
--total || close();
|
||||
it('should disconnect upon transport disconnection', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var chat = client(srv, '/chat');
|
||||
var news = client(srv, '/news');
|
||||
var total = 2;
|
||||
var totald = 2;
|
||||
var s;
|
||||
sio.of('/news', function(socket){
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
sio.of('/chat', function(socket){
|
||||
s = socket;
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
--total || close();
|
||||
--total || close();
|
||||
});
|
||||
sio.of('/chat', function(socket){
|
||||
s = socket;
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
function close(){
|
||||
s.disconnect(true);
|
||||
}
|
||||
--total || close();
|
||||
});
|
||||
function close(){
|
||||
s.disconnect(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should disconnect both default and custom namespace upon disconnect', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var lolcats = client(srv, '/lolcats');
|
||||
var total = 2;
|
||||
var totald = 2;
|
||||
var s;
|
||||
sio.of('/', function(socket){
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
--total || close();
|
||||
});
|
||||
sio.of('/lolcats', function(socket){
|
||||
s = socket;
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
--total || close();
|
||||
});
|
||||
function close(){
|
||||
s.disconnect(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should not crash while disconnecting socket', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv,'/ns');
|
||||
sio.on('connection', function(socket){
|
||||
socket.disconnect();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should disconnect both default and custom namespace upon disconnect', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var lolcats = client(srv, '/lolcats');
|
||||
var total = 2;
|
||||
var totald = 2;
|
||||
var s;
|
||||
sio.of('/', function(socket){
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
--total || close();
|
||||
});
|
||||
sio.of('/lolcats', function(socket){
|
||||
s = socket;
|
||||
socket.on('disconnect', function(reason){
|
||||
--totald || done();
|
||||
});
|
||||
--total || close();
|
||||
});
|
||||
function close(){
|
||||
s.disconnect(true);
|
||||
}
|
||||
it('should return error connecting to non-existent namespace', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var socket = client(srv,'/doesnotexist');
|
||||
socket.on('error', function(err) {
|
||||
expect(err).to.be('Invalid namespace');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('socket', function(){
|
||||
|
||||
it('should not fire events more than once after manually reconnecting', function(done) {
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var clientSocket = client(srv, { reconnection: false });
|
||||
clientSocket.on('connect', function init() {
|
||||
clientSocket.removeListener('connect', init);
|
||||
clientSocket.io.engine.close();
|
||||
|
||||
clientSocket.connect();
|
||||
clientSocket.on('connect', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should not fire reconnect_failed event more than once when server closed', function(done) {
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
srv.listen(function(){
|
||||
var clientSocket = client(srv, { reconnectionAttempts: 3, reconnectionDelay: 10 });
|
||||
clientSocket.on('connect', function() {
|
||||
srv.close();
|
||||
});
|
||||
|
||||
clientSocket.on('reconnect_failed', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should receive events', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
@@ -619,6 +757,30 @@ describe('socket.io', function(){
|
||||
});
|
||||
});
|
||||
|
||||
it('should receive all events emitted from namespaced client immediately and in order', function(done) {
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
var total = 0;
|
||||
srv.listen(function(){
|
||||
sio.of('/chat', function(s){
|
||||
s.on('hi', function(letter){
|
||||
total++;
|
||||
if (total == 2 && letter == 'b') {
|
||||
done();
|
||||
} else if (total == 1 && letter != 'a') {
|
||||
throw new Error('events out of order');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var chat = client(srv, '/chat');
|
||||
chat.emit('hi', 'a');
|
||||
setTimeout(function() {
|
||||
chat.emit('hi', 'b');
|
||||
}, 50);
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit events with callbacks', function(done){
|
||||
var srv = http();
|
||||
var sio = io(srv);
|
||||
@@ -1115,9 +1277,14 @@ describe('socket.io', function(){
|
||||
expect(s.rooms).to.eql([s.id, 'a']);
|
||||
s.join('b', function(){
|
||||
expect(s.rooms).to.eql([s.id, 'a', 'b']);
|
||||
s.leave('b', function(){
|
||||
expect(s.rooms).to.eql([s.id, 'a']);
|
||||
done();
|
||||
s.join( 'c', function(){
|
||||
expect(s.rooms).to.eql([s.id, 'a', 'b', 'c']);
|
||||
s.leave('b', function(){
|
||||
expect(s.rooms).to.eql([s.id, 'a', 'c']);
|
||||
s.leaveAll();
|
||||
expect(s.rooms).to.eql([]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user