gpt4 book ai didi

node.js - Passport.socketio 在查找 session 时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:50 25 4
gpt4 key购买 nike

我正在尝试从套接字访问 session ,但似乎无法建立连接。如果没有失败,授权就会失败,并且我会收到带有以下消息的失败回调:

failed connection to socket.io: No session found

我将把所有代码放在这里,以便更容易发现我做错了什么。

var express  = require('express');
var app = express();
var http = require('http');
var socketio = require('socket.io')
var passportSocketIo = require('passport.socketio');
var port = process.env.PORT || 3000;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');

var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);

var server = http.createServer(app);
var io = socketio.listen(server);

var dbConfig = require('./config/database.js');
mongoose.connect(dbConfig.url);
var sessionStore = new MongoStore({ db: mongoose.connection.db });

require('./config/passport')(passport);

app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.set('view engine', 'ejs');

app.use(session({
key: 'connect.sid',
secret: 'secret',
store: sessionStore,
resave: true,
saveUninitialized: true
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(express.static(__dirname + '/public'));

require('./app/routes.js')(app, passport);

server.listen(3000, function() {
console.log('App running on port: ' + port);
});

io.use(passportSocketIo.authorize({
passport: passport,
cookieParser: cookieParser,
key: 'connect.sid',
secret: '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);
}

io.sockets.on('connection', function(socket) {
var date = new Date();
var time = date.getHours() + ":" + date.getMinutes();
socket.emit('message', {username: 'Server', message: 'welcome to the chat'});
socket.on('send', function(data) {
io.sockets.emit('message', data);
});
});

我应该如何与 socket.io 建立与 session 的连接?

此外,我还发现用户数据是通过 socket.handshake.user 访问的。在这种情况下这是正确的吗?

为了清楚起见,版本如下:

express: 4.8.5
passport: 0.2.0
socket.io: 1.0.6
passport.socketio: 3.2.0

编辑

问题的一部分似乎是 localhost 与已经存在的 127.0.0.1 错误有关。但是,现在我没有收到任何握手数据。

最佳答案

首先,正如我在问题编辑中所指出的,请务必直接输入 IP,而不是使用 localhost:127.0.0.1。这是一个已知错误,当​​您指向 localhost 时,该错误将不允许您正确发送 cookie。

Passport socket Github page最后,针对 socket.io 1.x 的授权回调更新了以下内容。

function onAuthorizeSuccess(data, accept){
console.log('successful connection to socket.io');
accept();
}

function onAuthorizeFail(data, message, error, accept){
console.log('failed connection to socket.io:', data, message);
if(error)
accept(new Error(message));
}

此外,握手数据不再存储在同一位置。将 socket.handshake.user 替换为 socket.request.user

关于node.js - Passport.socketio 在查找 session 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25456656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com