mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-12 08:27:57 -05:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65ece01135 | ||
|
|
db0c69969e | ||
|
|
94df7bcdfd | ||
|
|
9a014e2df4 | ||
|
|
2b10f1b3a4 | ||
|
|
a10dc8d92d | ||
|
|
2b216902e1 |
13
History.md
13
History.md
@@ -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
|
||||
===================
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
[](https://david-dm.org/socketio/socket.io#info=devDependencies)
|
||||
[](https://www.npmjs.com/package/socket.io)
|
||||

|
||||
[](http://slack.socket.io)
|
||||
[](https://slackin-socketio.now.sh)
|
||||
|
||||
## Features
|
||||
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
module.exports = {
|
||||
entry: './lib/index.js',
|
||||
output: {
|
||||
path: './dist',
|
||||
path: require('path').join(__dirname, '../dist'),
|
||||
filename: 'app.js'
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
33
examples/webpack-build/support/webpack.config.json-parser.js
Normal file
33
examples/webpack-build/support/webpack.config.json-parser.js
Normal 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'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
@@ -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: [
|
||||
|
||||
45
lib/index.js
45
lib/index.js
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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){
|
||||
|
||||
Reference in New Issue
Block a user