https://github.com/xpepermint/socket.io-express-session 这是我尝试过的众多方法之一。 我也尝试了所有这些答案: How to share sessions with Socket.IO 1.x and Express 4.x? 还有一些......这一切都给了我相同的结果:
抛出新的TypeError(“参数'url'必须是字符串,而不是”+ typeof URL) ^ TypeError:参数'url'必须是字符串,而不是未定义
我已经尝试过我在互联网上找到的所有解决方案,基本上一切都会让我遇到同样的错误。 在某些时候,我只是尝试复制和粘贴找到的例子,他们给我完全相同的结果。 我猜其中一个中间件更新后出了问题。怎么解决?是否有另一种可靠的方式与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);
});
上面是一个返回此错误的示例。
答案 0 :(得分:4)
使用io.use(...)
时,会出现错误问题是会话中间件期望在req.orginalUrl中找到会话。套接字没有orginalUrl。要解决这个问题,只需指定一个......
io.use(function(socket, next){
socket.request.originalUrl = socket.request.url;
session(socket.request, socket.request.res, next);
});
下一个问题是会话(...)希望解析cookie。我们可以使用Cookie-parser'为此:
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);
});
答案 1 :(得分:0)
使用Bradley 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);