0
js,express,redis(用於內存存儲)和socket.io。我正在嘗試設置通過身份驗證的套接字調用以便與每個用戶的套接字匹配。我有這樣的代碼,除了某些原因會話ID顯示爲未定義並獲取錯誤加載會話(握手錯誤)之外,大部分都會這樣做。會話ID顯示爲未定義node.js
下面是代碼:
var io = require('socket.io');
var notificationsN = io.listen(server);
var RedisStore = require('connect-redis')(express);
var redis = require("redis").createClient();
var cookie = require('cookie');
var session_store = new RedisStore({client: redis});
app.use(express.session({
secret: "secret",
store: new RedisStore({ host: 'localhost', port: 3000, client: redis })
}));
notificationsN.configure(function() {
notificationsN.set('authorization', function(data, accept) {
console.log('data below');
console.log(data);
console.log('data sid below');
console.log(data.sessionID);
if(!data.headers.cookie) return accept(new Error('No user cookie found'), true);
data.cookie = cookie.parse(data.headers.cookie, { secure: true });
data.cookie = connect.utils.parseSignedCookies(data.cookie, config.session.secret);
data.cookie = connect.utils.parseJSONCookies(data.cookie);
data.sessionID = data.cookie[config.session.key];
session_store.load(data.sessionID, function(err, session){
if(err || !session) return accept(new Error('Error loading session'), false);
data.session = session;
return accept(null, true);
});
console.log(data);
})
})
notificationsN.on('connection', function(socket) {
socket.on('message', function(msg) {
console.log(msg);
});
socket.on('disconnect', function() {
console.log('disconnecting from redis');
});
});
以下是我找回從console.log(data)
:
{ headers:
{ host: 'localhost:3000',
connection: 'keep-alive',
'cache-control': 'max-age=0',
'user-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTM
L, like Gecko) Chrome/29.0.1547.57 Safari/537.36',
accept: '*/*',
referer: 'http://localhost:3000/userProfile',
'accept-encoding': 'gzip,deflate,sdch',
'accept-language': 'en-US,en;q=0.8',
cookie: 'connect.sid=crazyvalue; __atuvc=crazyvalue; userid=crazyvalue' },
address: { address: 'ip', port: port},
time: 'Fri Aug 30 2013 15:50:31 GMT-0400 (Eastern Daylight Time)',
query: { t: '1377892231624' },
url: '/socket.io/1/?t=1377892231624',
xdomain: false,
secure: undefined,
issued: 1377892231630,
cookie: {},
sessionID: undefined }
所以不知道爲什麼,會話ID是不確定的和安全的節目爲未定義。
我這樣做,但它仍然打印出未定義的相同值 – Lion789
至少這是有問題的,所以我仍然離開它。你似乎正在改變data.cookie很多。什麼調用JSON.stringify(data.cookie,0,4)讓你每一步? – ChrisCM
我第一次打電話給cookie.parse(它顯示了cookie),但之後它只顯示爲{} – Lion789