Passport.socketio有发现问题的问题 [英] Passport.socketio has issues finding session
问题描述
我尝试从套接字访问会话,但似乎无法建立连接。没有失败,授权失败,我收到失败回调,并显示以下消息:
I am trying to access the session from sockets, but can't seem to make a connection. Without fail, authorization fails and I get the fail callback with the following message:
failed connection to socket.io: No session found
我将把我所有的代码放在这里,以便更容易找出我做错了。
I will place all my code here so that it might be easier to spot what I'm doing wrong.
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建立与会话的连接?
How should I be establishing a connection to the session with socket.io?
此外,我看到用户数据通过 socket.handshake.user
访问。在这种情况下是否正确?
Also, I have seen that the user data is accessed via socket.handshake.user
. Would that be correct in this case?
为了清楚起见,版本如下:
And for clarity, the versions are as follows:
express: 4.8.5
passport: 0.2.0
socket.io: 1.0.6
passport.socketio: 3.2.0
编辑
问题是已经存在的 localhost
与 127.0.0.1
错误。然而,现在我没有得到任何握手数据。
It appears that part of the issue was the localhost
versus 127.0.0.1
bug that already exists. However, now I don't get any handshake data back.
推荐答案
首先,正如我在我的问题编辑中指出的,请确保直接键入IP,而不是使用 localhost
: 127.0.0.1
。这是一个已知的错误,不能允许您在指向 localhost
时正确发送cookie。
First off, as I had noted in my question edit, be sure to type in the IP directly, rather than using localhost
: 127.0.0.1
. This is a known bug that will not allow you to send the cookie over correctly when pointing to localhost
.
护照 - socketio Github页面最后更新了套接字的授权回调。 io 1.x。
The passport-socketio Github page was, at last, updated with the following for the authorization callbacks for 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
。
Also, the handshake data is no longer stored in the same place. Replace socket.handshake.user
with socket.request.user
.
这篇关于Passport.socketio有发现问题的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!