From d9996f0470cef477999e747bc90b645a4c37717c Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Sun, 7 Dec 2014 05:56:08 +0200 Subject: [PATCH 1/8] Added tests for volatile --- test/socket.io.js | 206 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 204 insertions(+), 2 deletions(-) diff --git a/test/socket.io.js b/test/socket.io.js index 8e66564ab2..87293ea6a5 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -447,7 +447,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 +456,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); @@ -800,6 +800,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); From 17960ed038c613dccd8f6b99872008021730db12 Mon Sep 17 00:00:00 2001 From: Badarau Petru Date: Mon, 22 Dec 2014 07:17:01 +0200 Subject: [PATCH 2/8] Update Readme.md underylings to underlyings --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 271ac43169..6bf1f09e0d 100644 --- a/Readme.md +++ b/Readme.md @@ -274,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 From 8242dd01ef51441df1e342c26ca83f56bf91b0e7 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 26 Dec 2014 14:16:27 +0200 Subject: [PATCH 3/8] package: debug v2.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a258126a51..52b11baf9f 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "socket.io-client": "1.2.1", "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", From a116d7765a6288eada3ece3574328e2e564a53d8 Mon Sep 17 00:00:00 2001 From: Drew Blaisdell Date: Fri, 9 Jan 2015 16:30:08 -0800 Subject: [PATCH 4/8] 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. --- lib/index.js | 2 +- test/socket.io.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 92170c3938..a4c7f3340c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -62,7 +62,7 @@ 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); diff --git a/test/socket.io.js b/test/socket.io.js index 8e66564ab2..cda24ac4a9 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -302,6 +302,21 @@ 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(); + }); + }); }); describe('close', function(){ From ca82c09bf21dee5be3f40f0969f2df0cf3d6a7d3 Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Sat, 10 Jan 2015 14:58:50 -0800 Subject: [PATCH 5/8] fix leaving unknown rooms close #1670 --- lib/socket.js | 5 ++++- test/socket.io.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/socket.js b/lib/socket.js index 4a3aa3d7a0..38440d1f42 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -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; diff --git a/test/socket.io.js b/test/socket.io.js index c19172b38d..98f9aa7084 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -1400,6 +1400,30 @@ 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){ From d9415a38e45636aa29a8af3eb7389d6d25d13792 Mon Sep 17 00:00:00 2001 From: Willson Mock Date: Sun, 11 Jan 2015 00:11:55 -0500 Subject: [PATCH 6/8] update license with up-to-date year range --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 81a927588c..b248ba1bc5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ (The MIT License) -Copyright (c) 2014 Automattic +Copyright (c) 2014-2015 Automattic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the From 0523b655da99241dba9beaeb7af34a4d08434f27 Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Wed, 14 Jan 2015 17:22:50 +0200 Subject: [PATCH 7/8] Add test for reconnection after server restarts --- test/socket.io.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/test/socket.io.js b/test/socket.io.js index 98f9aa7084..0363fadfd9 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -462,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); @@ -471,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); @@ -1101,6 +1101,32 @@ 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'); + }); + }); + }); + }); + }); + }); }); describe('messaging many', function(){ From f5b75151bdb0d039b01a83c77a0800490c524654 Mon Sep 17 00:00:00 2001 From: Alexey Chuvashov Date: Thu, 15 Jan 2015 01:24:19 +0300 Subject: [PATCH 8/8] Add space in error message --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index a4c7f3340c..64de3b880a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -194,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); }