socket.io过度到1.0以上版本和原有配置有些区别。特在此简单强调下:
1、Authentication differences (关于身份验证差异)
Socket.io uses middleware now
You can give a Socket.io server arbitrary functions via io.use() that are run when a socket is created. Check out this example:
var srv = require('http').createServer();
var io = require('socket.io')(srv);
var run = 0;
io.use(function(socket, next){
run++; // 0 -> 1
next();
});
io.use(function(socket, next) {
run++; // 1 -> 2
next();
});
var socket = require('socket.io-client')();
socket.on('connect', function(){
// run == 2 at this time
});
… so its cleaner to do auth via middleware now
The old io.set() and io.get() methods are deprecated and only supported for backwards compatibility. Here is a translation of an old authorization example into middleware-style.
io.set('authorization', function (handshakeData, callback) { ** //socket.io <=0.9 版本方式**
// make sure the handshake data looks good
callback(null, true); // error first, 'authorized' boolean second
});
vs.
io.use(function(socket, next) { ** //socket.io >=1.0 版本方式**
var handshakeData = socket.request;
// make sure the handshake data looks good as before
// if error do this:
// next(new Error('not authorized');
// else just call next
next();
});
Namespace authorization?
io.of('/namespace').use(function(socket, next) {
var handshakeData = socket.request;
next();
});
2、Configuration differences(配置差异)
io.set is gone
Instead do configuration in server initialization like this:
var socket = require('socket.io')({
// options go here
});
Options like log-level are gone. io.set('transports'), io.set('heartbeat interval'), io.set('heartbeat timeout', and io.set('resource') are still supported for backwards compatibility.
Setting resource path
The previous resource option is equivalent to the new path option, but needs a / in the beginning. For example, the following configuration
var socket = io.connect('localhost:3000', {
'resource': 'path/to/socket.io';
});
becomes
var socket = io.connect('localhost:3000', {
'path': '/path/to/socket.io';
});