问题描述
使用connect,express和socket.io,我试图允许我的应用程序在重新连接时获取会话详细信息。我的会话显然在客户端连接时工作,但是如果我在浏览器上刷新页面,则会忘记所有内容。
我的会话cookie绝对相同,所以不是这样。
Using connect, express, and socket.io, I'm trying to allow my application to grab the session details when reconnecting. My sessions obviously work while the client is connected, but if I refresh the page on my browser, it forgets everything.
My session cookie is definitely the same, so it's not that.
我的代码是从许多不同的来源获取的一个很大的mish-mash的片段,因为似乎没有一个完整的示例应用程序。 :/ /
My code's a big mish-mash of snippets I've taken from a number of different sources, since there doesn't seem to be one complete example application out there. :-/
我缺少什么..?
var qs = require('querystring'),
express = require('express'),
app = express.createServer(),
io = require('socket.io').listen(app.listen(8000)),
routes = require('./routes'),
pCookie = require('connect').utils.parseCookie,
Session = require('connect').middleware.session.Session,
RedStore= require('connect-redis')(express),
sStore = new RedStore();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ store: sStore, secret: 'tehsecretz' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
io.sockets.on('connection', function (client) {
var hs = client.handshake,
session = hs.session;
console.log('A socket connected called: ' + hs.sessionId);
var intervalId = setInterval(function() {
hs.session.reload(function() {
hs.session.touch().save();
});
}, 60 * 1000);
if (!session.userName) {
// Prompts the user for a name on the frontend
client.emit('need-to-register');
}
client.on('message', function(msg, c) {
console.log(session);
console.log(msg);
});
client.on('register-user', function(data, fn) {
// This retrieves the user's name
// and - hopefully - saves it to the session.
data = qs.parse(data);
session.userName = data.username;
hs.session.save();
console.log('Saving: ', session);
fn('ok');
});
client.on('disconnect', function() {
clearInterval(intervalId);
});
});
io.set('authorization', function (data, accept) {
if (data.headers.cookie) {
data.cookie = pCookie(data.headers.cookie);
data.sessionId = data.cookie['connect.sid'];
data.sessionStore = sStore;
sStore.get(data.sessionId, function (err, session) {
if (err || !session) {
accept('Error', false);
} else {
console.log(data.sessionId, session);
data.session = new Session(data, session);
accept(null, true);
}
});
} else {
return accept('No cookie transmitted', false);
}
});
感谢任何帮助提供!
推荐答案
呃所以在上,他引用了标识符 sessionID
。我认为这是一个很差的惯例(因为我习惯于camelCase),并在我的代码中及时更改为 sessionId
。
Ugh. So in Daniel Baulig's post on the subject, he referenced the identifier sessionID
. I figured that was just poor convention (as I'm used to camelCase) and promptly changed it to sessionId
in my code.
当我正在调试时,我打开了我的redis实例上的 MONITOR
,注意到会话被写入 sess:undefined
。
As I was debugging, I turned on MONITOR
ing on my redis instance and noticed the session was being written to sess:undefined
.
结果 sessionID
是特殊的,在内部被引用为实际标识符。将 sessionId
的所有实例更改为 sessionID
可以让我有更好的线索来确定谁在连接!
Turns out sessionID
is special and is referenced internally as the actual identifier. Changing all instances of sessionId
to sessionID
allows me to have a better clue as to who is connecting!
这篇关于使用express / connect-redis抓住页面重新加载的现有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!