我正在将node.js与socket.io和redisStore用于我的快速会话。在我的套接字握手,我想读取cookie以获取会话cookie,所以我能够读取和写入会话。 这是我到目前为止所得到的:
在我的app.js中:
var express = require('express');
var app = express();
var redis = require('redis');
var redisClient = redis.createClient(6379, 'localhost');
server = http.createServer(app);
server.listen(8080, function() {
console.log('listening on *:8080');
});
var io = require('./routes/index').listen(server);
var routes = require('./routes/index').router;
app.use(session({
secret: 'my-secret-string',
key: 'express.sid',
cookie: { maxAge: 2628000000 },
store: new (require('express-sessions'))({
storage: 'redis',
instance: redisClient,
host: 'localhost',
port: 6379,
}),
resave: true,
saveUninitialized: true
}));
app.use('/', routes);
module.exports = app;
和我的index.js:
var express = require('express');
var router = express.Router();
var socketio = require('socket.io');
var io;
router.get('/', function(req, res) {
//do stuff here...
res.render('index', { params: params });
}
var listen = function(app) {
io = socketio.listen(app);
io.sockets.on('connection', function (socket) {
console.log(socket.handshake); // there is an handshake object with cookie information
// which i can parse the express.sid (=sessionID) from
socket.on('someEvent', function(args, callback) {
//do stuff here
callback(err, msg);
});
});
return io;
}
module.exports = {
listen: listen,
router: router
}
现在,当客户端发出" someEvent"时,我想从会话cookie中读取会话ID并对会话数据进行处理。
我关注了this指南和this,但它们似乎已经过时或者不适合我的设置。 到目前为止没有运气。非常感谢帮助!这是我的第一个node.js项目..