2016-01-23 73 views
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握手。我該如何解決這個問題?

回答

0

在我已經瀏覽了express-session文檔之後,發現它的關鍵問題。

我已經改變

express.sid => 'connect.sid' 

io.use(passportSocketIo.authorize({ 
    key: 'connect.sid'  
})); 

固定的問題,好像默認快遞將提供「connect.sid」。

相關問題