2015-07-28 126 views
1

https://github.com/xpepermint/socket.io-express-session 這是我嘗試過的很多方法之一。 我嘗試了所有這些問題的答案,以及: How to share sessions with Socket.IO 1.x and Express 4.x? 而一些......這一切都使我有exaclty相同的結果:將快速會話傳遞給socket.io

throw new TypeError("Parameter 'url' must be a string, not " + typeof url) ^TypeError: Parameter 'url' must be a string, not undefined

我已經想盡解決方案,我發現在互聯網上,基本上一切都使我到相同的錯誤。 在某些時候,我只是試着複製和粘貼找到的例子,他們給我完全相同的結果。 我猜中間件更新之後出了點問題。如何解決它?有沒有另一種可靠的方式與socket.io共享快速會話?

var Session = require('express-session'); 
var session = Session({ secret: 'pass', resave: true, saveUninitialized: true }); 
var cookieParser = require('cookie-parser'); 

var express = require('express'); 
var app = express(); 


app.use(cookieParser()); 
app.use(session); // session support 
app.get('/', function (req, res) { 
    req.session.uid = 1; 
    res.send('Hi user #' + req.session.uid); 
}); 


var http = require('http').createServer(app); 
http.listen(3000, function(){ 
    console.log('listening on *:3000'); 
}); 

var ios = require('socket.io-express-session'); 
var io = require('socket.io')(http); 
io.use(ios(session)); // session support 
io.on('connection', function(socket){ 
    console.log(socket.handshake.session); 
}); 

上面是一個返回這個錯誤的例子。

回答

4

的錯誤,當您使用io.use(...)

的問題是,會話中間件希望找到在req.orginalUrl會話發生在階段。套接字沒有orginalUrl。爲了解決這個問題,只要將一個...

io.use(function(socket, next){ 
    socket.request.originalUrl = socket.request.url; 
    session(socket.request, socket.request.res, next); 
}); 

下一個問題將是這屆會議(...)預計,cookies來進行解析。我們可以用「餅乾分析器」這個:

var cookieParse = cookieParser(); 
    io.use(function(socket, next){ 
    cookieParse(socket.request, socket.request.res, next); 
}); 

之後,我們可以簡單地橋護照和socket.io:

var passInit = passport.initialize(); 
var passSess = passport.session(); 
io.use(function(socket, next){ 
    passInit(socket.request, socket.request.res, next); 
}); 
io.use(function(socket, next){ 
    passSess(socket.request, socket.request.res, next); 
}); 

現在我們io.on(「連接」),我們可以請參閱socket.request.user

io.on('connection', function(socket){ 
    console.log("socket connection"); 
    console.log(socket.request.user) 
    console.log(socket.request.session.passport.user); 
}); 
0

使用布拉德利Lederholz的答案,這是我如何使自己的工作。請參閱Bradley Lederholz的答案,以獲取更多解釋。

var app = express(); 
var server = require('http').createServer(app); 
var io = require('socket.io'); 
var cookieParse = require('cookie-parser')(); 
var passport = require('passport'); 
var passportInit = passport.initialize(); 
var passportSession = passport.session(); 
var session = require('express-session'); 
var mongoStore = require('connect-mongo')(session); 
var mongoose = require('mongoose'); 
var sessionMiddleware = session({ 
    secret: 'some secret', 
    key: 'express.sid', 
    resave: true, 
    httpOnly: true, 
    secure: true, 
    ephemeral: true, 
    saveUninitialized: true, 
    cookie: {}, 
    store:new mongoStore({ 
    mongooseConnection: mongoose.connection, 
    db: 'mydb' 
    }); 
}); 

app.use(sessionMiddleware); 
io = io(server); 
io.use(function(socket, next){ 
    socket.client.request.originalUrl = socket.client.request.url; 
    cookieParse(socket.client.request, socket.client.request.res, next); 
}); 

io.use(function(socket, next){ 
    socket.client.request.originalUrl = socket.client.request.url; 
    sessionMiddleware(socket.client.request, socket.client.request.res, next); 
}); 

io.use(function(socket, next){ 
    passportInit(socket.client.request, socket.client.request.res, next); 
}); 

io.use(function(socket, next){ 
    passportSession(socket.client.request, socket.client.request.res, next); 
}); 

io.on('connection', function(socket){ 
    ... 
}); 

... 
server.listen(8000);