Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f57505f

Browse files
committed
Merge branch 'master' of github.com:Automattic/socket.io
2 parents f8f1b13 + b8ded0d commit f57505f

File tree

6 files changed

+278
-8
lines changed

6 files changed

+278
-8
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(The MIT License)
22

3-
Copyright (c) 2014 Automattic <[email protected]>
3+
Copyright (c) 2014-2015 Automattic <[email protected]>
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ server.listen(3000);
274274

275275
### Socket#conn:Socket
276276

277-
A reference to the underyling `Client` transport connection (engine.io
277+
A reference to the underlying `Client` transport connection (engine.io
278278
`Socket` object).
279279

280280
### Socket#request:Request

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Server.prototype.checkRequest = function(req, fn) {
6262
var origin = req.headers.origin || req.headers.referer;
6363

6464
// file:// URLs produce a null Origin which can't be authorized via echo-back
65-
if ('null' == origin) origin = '*';
65+
if ('null' == origin || null == origin) origin = '*';
6666

6767
if (!!origin && typeof(this._origins) == 'function') return this._origins(origin, fn);
6868
if (this._origins.indexOf('*:*') !== -1) return fn(null, true);
@@ -194,7 +194,7 @@ Server.prototype.origins = function(v){
194194
Server.prototype.listen =
195195
Server.prototype.attach = function(srv, opts){
196196
if ('function' == typeof srv) {
197-
var msg = 'You are trying to attach socket.io to an express' +
197+
var msg = 'You are trying to attach socket.io to an express ' +
198198
'request handler function. Please pass a http.Server instance.';
199199
throw new Error(msg);
200200
}

lib/socket.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ Socket.prototype.leave = function(room, fn){
242242
this.adapter.del(this.id, room, function(err){
243243
if (err) return fn && fn(err);
244244
debug('left room %s', room);
245-
self.rooms.splice(self.rooms.indexOf(room), 1);
245+
var idx = self.rooms.indexOf(room);
246+
if (idx >= 0) {
247+
self.rooms.splice(idx, 1);
248+
}
246249
fn && fn(null);
247250
});
248251
return this;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"socket.io-client": "Automattic/socket.io-client#6b97ec",
2525
"socket.io-adapter": "0.3.1",
2626
"has-binary-data": "0.1.3",
27-
"debug": "0.7.4"
27+
"debug": "2.1.0"
2828
},
2929
"devDependencies": {
3030
"mocha": "1.16.2",

test/socket.io.js

Lines changed: 269 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,21 @@ describe('socket.io', function(){
302302
done();
303303
});
304304
});
305+
306+
it('should allow request when origin defined as function and no origin is supplied', function(done) {
307+
var sockets = io({ origins: function(origin,callback){
308+
if (origin == '*') {
309+
return callback(null, true);
310+
}
311+
return callback(null, false);
312+
} }).listen('54021');
313+
request.get('http://localhost:54021/socket.io/default/')
314+
.query({ transport: 'polling' })
315+
.end(function (err, res) {
316+
expect(res.status).to.be(200);
317+
done();
318+
});
319+
});
305320
});
306321

307322
describe('close', function(){
@@ -447,7 +462,7 @@ describe('socket.io', function(){
447462
var c1 = client(srv, '/');
448463
var c2 = client(srv, '/abc');
449464
});
450-
465+
451466
it('should be equivalent for "" and "/" on client', function(done){
452467
var srv = http();
453468
var sio = io(srv);
@@ -456,7 +471,7 @@ describe('socket.io', function(){
456471
});
457472
var c1 = client(srv, '');
458473
});
459-
474+
460475
it('should work with `of` and many sockets', function(done){
461476
var srv = http();
462477
var sio = io(srv);
@@ -800,6 +815,208 @@ describe('socket.io', function(){
800815
});
801816
});
802817

818+
it('should not emit volatile event after regular event (polling)', function(done) {
819+
var srv = http();
820+
var sio = io(srv, { transports: ['polling'] });
821+
822+
var counter = 0;
823+
srv.listen(function(){
824+
sio.on('connection', function(s){
825+
s.emit('ev', 'data');
826+
s.volatile.emit('ev', 'data');
827+
});
828+
829+
var socket = client(srv, { transports: ['polling'] });
830+
socket.on('ev', function() {
831+
counter++;
832+
});
833+
});
834+
835+
setTimeout(function() {
836+
expect(counter).to.be(1);
837+
done();
838+
}, 200);
839+
});
840+
841+
it('should not emit volatile event after regular event (ws)', function(done) {
842+
var srv = http();
843+
var sio = io(srv, { transports: ['websocket'] });
844+
845+
var counter = 0;
846+
srv.listen(function(){
847+
sio.on('connection', function(s){
848+
s.emit('ev', 'data');
849+
s.volatile.emit('ev', 'data');
850+
});
851+
852+
var socket = client(srv, { transports: ['websocket'] });
853+
socket.on('ev', function() {
854+
counter++;
855+
});
856+
});
857+
858+
setTimeout(function() {
859+
expect(counter).to.be(1);
860+
done();
861+
}, 200);
862+
});
863+
864+
it('should emit volatile event (polling)', function(done) {
865+
var srv = http();
866+
var sio = io(srv, { transports: ['polling'] });
867+
868+
var counter = 0;
869+
srv.listen(function(){
870+
sio.on('connection', function(s){
871+
// Wait to make sure there are no packets being sent for opening the connection
872+
setTimeout(function() {
873+
s.volatile.emit('ev', 'data');
874+
}, 20);
875+
});
876+
877+
var socket = client(srv, { transports: ['polling'] });
878+
socket.on('ev', function() {
879+
counter++;
880+
});
881+
});
882+
883+
setTimeout(function() {
884+
expect(counter).to.be(1);
885+
done();
886+
}, 200);
887+
});
888+
889+
it('should emit volatile event (ws)', function(done) {
890+
var srv = http();
891+
var sio = io(srv, { transports: ['websocket'] });
892+
893+
var counter = 0;
894+
srv.listen(function(){
895+
sio.on('connection', function(s){
896+
// Wait to make sure there are no packets being sent for opening the connection
897+
setTimeout(function() {
898+
s.volatile.emit('ev', 'data');
899+
}, 20);
900+
});
901+
902+
var socket = client(srv, { transports: ['websocket'] });
903+
socket.on('ev', function() {
904+
counter++;
905+
});
906+
});
907+
908+
setTimeout(function() {
909+
expect(counter).to.be(1);
910+
done();
911+
}, 200);
912+
});
913+
914+
it('should emit only one consecutive volatile event (polling)', function(done) {
915+
var srv = http();
916+
var sio = io(srv, { transports: ['polling'] });
917+
918+
var counter = 0;
919+
srv.listen(function(){
920+
sio.on('connection', function(s){
921+
// Wait to make sure there are no packets being sent for opening the connection
922+
setTimeout(function() {
923+
s.volatile.emit('ev', 'data');
924+
s.volatile.emit('ev', 'data');
925+
}, 20);
926+
});
927+
928+
var socket = client(srv, { transports: ['polling'] });
929+
socket.on('ev', function() {
930+
counter++;
931+
});
932+
});
933+
934+
setTimeout(function() {
935+
expect(counter).to.be(1);
936+
done();
937+
}, 200);
938+
});
939+
940+
it('should emit only one consecutive volatile event (ws)', function(done) {
941+
var srv = http();
942+
var sio = io(srv, { transports: ['websocket'] });
943+
944+
var counter = 0;
945+
srv.listen(function(){
946+
sio.on('connection', function(s){
947+
// Wait to make sure there are no packets being sent for opening the connection
948+
setTimeout(function() {
949+
s.volatile.emit('ev', 'data');
950+
s.volatile.emit('ev', 'data');
951+
}, 20);
952+
});
953+
954+
var socket = client(srv, { transports: ['websocket'] });
955+
socket.on('ev', function() {
956+
counter++;
957+
});
958+
});
959+
960+
setTimeout(function() {
961+
expect(counter).to.be(1);
962+
done();
963+
}, 200);
964+
});
965+
966+
it('should emit regular events after trying a failed volatile event (polling)', function(done) {
967+
var srv = http();
968+
var sio = io(srv, { transports: ['polling'] });
969+
970+
var counter = 0;
971+
srv.listen(function(){
972+
sio.on('connection', function(s){
973+
// Wait to make sure there are no packets being sent for opening the connection
974+
setTimeout(function() {
975+
s.emit('ev', 'data');
976+
s.volatile.emit('ev', 'data');
977+
s.emit('ev', 'data');
978+
}, 20);
979+
});
980+
981+
var socket = client(srv, { transports: ['polling'] });
982+
socket.on('ev', function() {
983+
counter++;
984+
});
985+
});
986+
987+
setTimeout(function() {
988+
expect(counter).to.be(2);
989+
done();
990+
}, 200);
991+
});
992+
993+
it('should emit regular events after trying a failed volatile event (ws)', function(done) {
994+
var srv = http();
995+
var sio = io(srv, { transports: ['websocket'] });
996+
997+
var counter = 0;
998+
srv.listen(function(){
999+
sio.on('connection', function(s){
1000+
// Wait to make sure there are no packets being sent for opening the connection
1001+
setTimeout(function() {
1002+
s.emit('ev', 'data');
1003+
s.volatile.emit('ev', 'data');
1004+
s.emit('ev', 'data');
1005+
}, 20);
1006+
});
1007+
1008+
var socket = client(srv, { transports: ['websocket'] });
1009+
socket.on('ev', function() {
1010+
counter++;
1011+
});
1012+
});
1013+
1014+
setTimeout(function() {
1015+
expect(counter).to.be(2);
1016+
done();
1017+
}, 200);
1018+
});
1019+
8031020
it('should emit message events through `send`', function(done){
8041021
var srv = http();
8051022
var sio = io(srv);
@@ -1086,6 +1303,32 @@ describe('socket.io', function(){
10861303
});
10871304
});
10881305
});
1306+
1307+
it('should be able to emit after server close and restart', function(done){
1308+
var srv = http();
1309+
var sio = io(srv);
1310+
1311+
sio.on('connection', function(socket){
1312+
socket.on('ev', function(data){
1313+
expect(data).to.be('payload');
1314+
done();
1315+
});
1316+
});
1317+
1318+
srv.listen(function(){
1319+
var port = srv.address().port;
1320+
var clientSocket = client(srv, { reconnectionAttempts: 10, reconnectionDelay: 100 });
1321+
clientSocket.once('connect', function(){
1322+
srv.close(function(){
1323+
srv.listen(port, function(){
1324+
clientSocket.on('reconnect', function(){
1325+
clientSocket.emit('ev', 'payload');
1326+
});
1327+
});
1328+
});
1329+
});
1330+
});
1331+
});
10891332
});
10901333

10911334
describe('messaging many', function(){
@@ -1385,6 +1628,30 @@ describe('socket.io', function(){
13851628
});
13861629
});
13871630
});
1631+
1632+
it('should properly cleanup left rooms', function(done){
1633+
var srv = http();
1634+
var sio = io(srv);
1635+
1636+
srv.listen(function(){
1637+
var socket = client(srv);
1638+
sio.on('connection', function(s){
1639+
s.join('a', function(){
1640+
expect(s.rooms).to.eql([s.id, 'a']);
1641+
s.join('b', function(){
1642+
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1643+
s.leave('unknown', function(){
1644+
expect(s.rooms).to.eql([s.id, 'a', 'b']);
1645+
s.leaveAll();
1646+
expect(s.rooms).to.eql([]);
1647+
done();
1648+
});
1649+
});
1650+
});
1651+
});
1652+
});
1653+
});
1654+
13881655
});
13891656

13901657
describe('middleware', function(done){

0 commit comments

Comments
 (0)