我正在尝试按照docs中所述的socketio进行简单的令牌授权。但节点抛出:
TypeError: Object #<Server> has no method 'configure'
当我尝试使用io.configure
配置socketio时。知道为什么配置方法不可用吗?
var express = require('express')
, app = express()
, server = require('http').Server(app)
, io = require('socket.io')(server)
io.configure(function (){
io.set('authorization', function (handshakeData, callback) {
...
})
完整的堆栈跟踪:
/Users/administrator/dev/chat-example/lib/io.js:9
io.configure(function (){
^
TypeError: Object #<Server> has no method 'configure'
at module.exports (/Users/administrator/dev/chat-example/lib/io.js:9:6)
at Object.<anonymous> (/Users/administrator/dev/chat-example/app.js:4:29)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
答案 0 :(得分:6)
io.configure和io.set已被弃用。这是socket.io 1.x
的示例中间件io.use(function(socket, next) {
var handshake = socket.request;
if (!handshake) {
return next(new Error('[[error:not-authorized]]'));
}
cookieParser(handshake, {}, function(err) {
if (err) {
return next(err);
}
var sessionID = handshake.signedCookies['express.sid'];
db.sessionStore.get(sessionID, function(err, sessionData) {
if (err) {
return next(err);
}
console.log(sessionData);
next();
});
});
});