1
我試圖訪問會話對象像socket.io中的req.user,以便我可以做一些事情,我使用passport.socket.io
庫來做到這一點。然而,現在的問題是,我不斷收到failed connection to socket.io: No session found.
passport.socketio一直說 - 無法連接到socket.io:沒有發現會話
下面的代碼
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var MongoStore = require('connect-mongo/es5')(session);
var passport = require('passport');
var passportSocketIo = require("passport.socketio");
var config = require('./config/config');
var sessionStore = new MongoStore({ url: config.database, autoReconnect: true });
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(cookieParser());
app.use(session({
resave: true,
saveUninitialized: true,
secret: config.secret,
store: sessionStore
}));
app.use(passport.initialize());
app.use(passport.session());
io.use(passportSocketIo.authorize({
cookieParser: cookieParser,
key: express.sid, // Not really sure how to get this key?
secret: config.secret,
store: sessionStore,
success: onAuthorizeSuccess,
fail: onAuthorizeFail,
}));
function onAuthorizeSuccess(data, accept){
console.log('successful connection to socket.io');
accept(null, true);
}
function onAuthorizeFail(data, message, error, accept){
if(error)
throw new Error(message);
console.log('failed connection to socket.io:', message);
accept(null, false);
if(error)
accept(new Error(message));
}
io.connect('connection', function(socket) {
console.log("Something Here");
});
http.listen(3000);
它使運行該功能,因爲護照沒有作出與socket.io onAuthorizeFail
握手。我該如何解決這個問題?