Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Automattic/socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Jan 18, 2015
2 parents f8f1b13 + b8ded0d commit f57505f
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 8 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/socket.js
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -24,7 +24,7 @@
"socket.io-client": "Automattic/socket.io-client#6b97ec",
"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",
Expand Down
271 changes: 269 additions & 2 deletions test/socket.io.js
Expand Up @@ -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(){
Expand Down Expand Up @@ -447,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);
Expand All @@ -456,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);
Expand Down Expand Up @@ -800,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);
Expand Down Expand Up @@ -1086,6 +1303,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(){
Expand Down Expand Up @@ -1385,6 +1628,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){
Expand Down

0 comments on commit f57505f

Please sign in to comment.