Compare commits

...

7 Commits
2.0.1 ... 2.0.3

Author SHA1 Message Date
Damien Arrachequesne
65ece01135 [chore] Release 2.0.3 2017-06-12 14:05:46 +02:00
Damien Arrachequesne
db0c69969e [fix] Reset rooms object before broadcasting (#2970)
It seems packets could be delivered to wrong room in some case, if the
_rooms array was not reset before the next emit.
2017-06-12 14:01:35 +02:00
Damien Arrachequesne
94df7bcdfd [fix] Fix middleware initialization (#2969)
Fix "TypeError: Cannot convert undefined or null to object" when a
middleware is added before the engine is properly attached.
2017-06-12 14:00:48 +02:00
Ra'Shaun Stovall
9a014e2df4 [docs] Update slack badge (#2961)
Update badge to https://slackin-socketio.now.sh/badge.svg
2017-06-08 12:28:56 +02:00
Damien Arrachequesne
2b10f1b3a4 [docs] Update webpack example (#2960) 2017-06-03 13:53:03 +02:00
Damien Arrachequesne
a10dc8d92d [chore] Release 2.0.2 2017-06-01 14:01:24 +02:00
Damien Arrachequesne
2b216902e1 [fix] Fix timing issues with middleware (#2948)
Using a middleware could previously lead to a connecting client
receiving a connect event from the server before the server triggers
its own connect event.
2017-05-22 13:01:59 +02:00
12 changed files with 170 additions and 60 deletions

View File

@@ -1,4 +1,17 @@
2.0.3 / 2017-06-12
===================
* [fix] Reset rooms object before broadcasting (#2970)
* [fix] Fix middleware initialization (#2969)
* [docs] Update slack badge (#2961)
* [docs] Update webpack example (#2960)
2.0.2 / 2017-06-01
===================
* [fix] Fix timing issues with middleware (#2948)
2.0.1 / 2017-05-09
===================

View File

@@ -7,7 +7,7 @@
[![devDependency Status](https://david-dm.org/socketio/socket.io/dev-status.svg)](https://david-dm.org/socketio/socket.io#info=devDependencies)
[![NPM version](https://badge.fury.io/js/socket.io.svg)](https://www.npmjs.com/package/socket.io)
![Downloads](https://img.shields.io/npm/dm/socket.io.svg?style=flat)
[![](http://slack.socket.io/badge.svg?)](http://slack.socket.io)
[![](https://slackin-socketio.now.sh/badge.svg)](https://slackin-socketio.now.sh)
## Features

View File

@@ -1,5 +1,7 @@
var socket = require('socket.io-client')('http://localhost:3000');
import io from 'socket.io-client';
const socket = io('http://localhost:3000');
console.log('init');

View File

@@ -5,15 +5,17 @@
"scripts": {
"build": "webpack --config ./support/webpack.config.js",
"build-slim": "webpack --config ./support/webpack.config.slim.js",
"build-all": "npm run build && npm run build-slim"
"build-json-parser": "webpack --config ./support/webpack.config.json-parser.js",
"build-all": "npm run build && npm run build-slim && npm run build-json-parser"
},
"author": "Damien Arrachequesne",
"license": "MIT",
"dependencies": {
"socket.io-client": "^1.7.2"
"socket.io-client": "^2.0.2",
"socket.io-json-parser": "^2.1.0"
},
"devDependencies": {
"strip-loader": "^0.1.2",
"webpack": "^1.14.0"
"webpack": "^2.6.1"
}
}

View File

@@ -2,7 +2,7 @@
module.exports = {
entry: './lib/index.js',
output: {
path: './dist',
path: require('path').join(__dirname, '../dist'),
filename: 'app.js'
},
}
};

View File

@@ -0,0 +1,33 @@
var webpack = require('webpack');
module.exports = {
entry: './lib/index.js',
output: {
path: require('path').join(__dirname, '../dist'),
filename: 'app.json-parser.js'
},
// generate sourcemap
devtool: 'source-map',
plugins: [
// replace require('debug')() with an noop function
new webpack.NormalModuleReplacementPlugin(/debug/, process.cwd() + '/support/noop.js'),
// replace socket.io-parser with socket.io-json-parser
new webpack.NormalModuleReplacementPlugin(/socket\.io-parser/, 'socket.io-json-parser'),
// use uglifyJS (IE9+ support)
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
],
module: {
loaders: [
{
// strip `debug()` calls
test: /\.js$/,
loader: 'strip-loader?strip[]=debug'
}
]
}
};

View File

@@ -4,13 +4,9 @@ var webpack = require('webpack');
module.exports = {
entry: './lib/index.js',
output: {
path: './dist',
path: require('path').join(__dirname, '../dist'),
filename: 'app.slim.js'
},
externals: {
// replace JSON polyfill (IE6/IE7) with global JSON object
json3: 'JSON'
},
// generate sourcemap
devtool: 'source-map',
plugins: [

View File

@@ -8,8 +8,7 @@ var read = require('fs').readFileSync;
var path = require('path');
var exists = require('fs').existsSync;
var engine = require('engine.io');
var client = require('socket.io-client');
var clientVersion = require('socket.io-client/package').version;
var clientVersion = require('socket.io-client/package.json').version;
var Client = require('./client');
var Emitter = require('events').EventEmitter;
var Namespace = require('./namespace');
@@ -245,31 +244,45 @@ Server.prototype.attach = function(srv, opts){
// set origins verification
opts.allowRequest = opts.allowRequest || this.checkRequest.bind(this);
var self = this;
if (this.sockets.fns.length > 0) {
this.initEngine(srv, opts);
return this;
}
var self = this;
var connectPacket = { type: parser.CONNECT, nsp: '/' };
this.encoder.encode(connectPacket, function (encodedPacket){
// the CONNECT packet will be merged with Engine.IO handshake,
// to reduce the number of round trips
opts.initialPacket = encodedPacket;
// initialize engine
debug('creating engine.io instance with opts %j', opts);
self.eio = engine.attach(srv, opts);
// attach static file serving
if (self._serveClient) self.attachServe(srv);
// Export http server
self.httpServer = srv;
// bind to engine events
self.bind(self.eio);
self.initEngine(srv, opts);
});
return this;
};
/**
* Initialize engine
*
* @param {Object} options passed to engine.io
* @api private
*/
Server.prototype.initEngine = function(srv, opts){
// initialize engine
debug('creating engine.io instance with opts %j', opts);
this.eio = engine.attach(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);
};
/**
* Attaches the static file serving.
*

View File

@@ -99,6 +99,10 @@ Namespace.prototype.initAdapter = function(){
*/
Namespace.prototype.use = function(fn){
if (this.server.eio) {
debug('removing initial packet');
delete this.server.eio.initialPacket;
}
this.fns.push(fn);
return this;
};

View File

@@ -139,38 +139,42 @@ Socket.prototype.buildHandshake = function(query){
Socket.prototype.emit = function(ev){
if (~exports.events.indexOf(ev)) {
emit.apply(this, arguments);
} else {
var args = Array.prototype.slice.call(arguments);
var packet = {
type: parser.EVENT,
data: args
};
return this;
}
// access last argument to see if it's an ACK callback
if (typeof args[args.length - 1] === 'function') {
if (this._rooms.length || this.flags.broadcast) {
throw new Error('Callbacks are not supported when broadcasting');
}
debug('emitting packet with ack id %d', this.nsp.ids);
this.acks[this.nsp.ids] = args.pop();
packet.id = this.nsp.ids++;
}
var args = Array.prototype.slice.call(arguments);
var packet = {
type: parser.EVENT,
data: args
};
// access last argument to see if it's an ACK callback
if (typeof args[args.length - 1] === 'function') {
if (this._rooms.length || this.flags.broadcast) {
this.adapter.broadcast(packet, {
except: [this.id],
rooms: this._rooms,
flags: this.flags
});
} else {
// dispatch packet
this.packet(packet, this.flags);
throw new Error('Callbacks are not supported when broadcasting');
}
// reset flags
this._rooms = [];
this.flags = {};
debug('emitting packet with ack id %d', this.nsp.ids);
this.acks[this.nsp.ids] = args.pop();
packet.id = this.nsp.ids++;
}
var rooms = this._rooms.slice(0);
var flags = assign({}, this.flags);
// reset flags
this._rooms = [];
this.flags = {};
if (rooms.length || flags.broadcast) {
this.adapter.broadcast(packet, {
except: [this.id],
rooms: rooms,
flags: flags
});
} else {
// dispatch packet
this.packet(packet, flags);
}
return this;
};
@@ -297,9 +301,10 @@ Socket.prototype.onconnect = function(){
debug('socket connected - writing packet');
this.nsp.connected[this.id] = this;
this.join(this.id);
// the CONNECT packet for the default namespace
// has already been sent as an `initialPacket`
if (this.nsp.name !== '/') {
var skip = this.nsp.name === '/' && this.nsp.fns.length === 0;
if (skip) {
debug('packet already sent in initial handshake');
} else {
this.packet({ type: parser.CONNECT });
}
};

View File

@@ -1,6 +1,6 @@
{
"name": "socket.io",
"version": "2.0.1",
"version": "2.0.3",
"description": "node.js realtime framework server",
"keywords": [
"realtime",
@@ -28,7 +28,7 @@
"engine.io": "~3.1.0",
"object-assign": "~4.1.1",
"socket.io-adapter": "~1.1.0",
"socket.io-client": "2.0.1",
"socket.io-client": "~2.0.2",
"socket.io-parser": "~3.1.1"
},
"devDependencies": {

View File

@@ -87,6 +87,9 @@ describe('socket.io', function(){
srv.set('authorization', function(o, f) { f(null, false); });
var socket = client(httpSrv);
socket.on('connect', function(){
expect().fail();
});
socket.on('error', function(err) {
expect(err).to.be('Not authorized');
done();
@@ -2145,6 +2148,9 @@ describe('socket.io', function(){
});
srv.listen(function(){
var socket = client(srv);
socket.on('connect', function(){
done(new Error('nope'));
});
socket.on('error', function(err){
expect(err).to.be('Authentication error');
done();
@@ -2163,6 +2169,9 @@ describe('socket.io', function(){
});
srv.listen(function(){
var socket = client(srv);
socket.on('connect', function(){
done(new Error('nope'));
});
socket.on('error', function(err){
expect(err).to.eql({ a: 'b', c: 3 });
done();
@@ -2186,6 +2195,26 @@ describe('socket.io', function(){
});
});
it('should only call connection after (lengthy) fns', function(done){
var srv = http();
var sio = io(srv);
var authenticated = false;
sio.use(function(socket, next){
setTimeout(function () {
authenticated = true;
next();
}, 300);
});
srv.listen(function(){
var socket = client(srv);
socket.on('connect', function(){
expect(authenticated).to.be(true);
done();
});
});
});
it('should be ignored if socket gets closed', function(done){
var srv = http();
var sio = io(srv);
@@ -2239,6 +2268,19 @@ describe('socket.io', function(){
});
});
});
it('should disable the merge of handshake packets', function(done){
var srv = http();
var sio = io();
sio.use(function(socket, next){
next();
});
sio.listen(srv);
var socket = client(srv);
socket.on('connect', function(){
done();
});
});
});
describe('socket middleware', function(done){