问题描述
不幸的是,socket.io开发团队决定弃用函数set()和get()。问题是这两个函数允许我们将变量保存到会话中。
Unfortunately socket.io developer team decided to deprecate functions set() and get(). The problem is that these two functions allowed us to save variable into session.
所以我的问题是:socket.io 1.0.5上的下列代码的等价物是什么?
So my question is : What is the equivalent of the folloing code on socket.io 1.0.5 ?
socket.set('mySessionVar', 'myValue');
socket.get('mySessionVar', function (error, mySessionVar) {
console.log('I have a super variable save in the session : '+mySessionVar);
socket.emit('mySessionVar', mySessionVar);
});
感谢您的帮助,
Guillaume。
Thank you for your help,Guillaume.
推荐答案
是socket.io 1.x的会话中间件。它建立在快速会话和cookie解析器之上。我知道我们正在谈论socket.io而不是表达,但它仍然适用于socket.io。此示例将使您的会话在redis商店中生效。
socket.io-handshake is session middleware for socket.io 1.x. It is built on top of express-session and cookie-parser. I know we are talking about socket.io and not express, but it still works with socket.io. This example will make your sessions live on a redis store.
var expressSession = require('express-session');
var connectRedis = require('connect-redis')(expressSession);
var cookieParser = require('cookie-parser');
var config = { session: { secret:'secret', key: 'bus.io', store: new connectRedis() } };
var handshake = require('socket.io-handshake');
var io = require('socket.io')(3000);
io.use(handshake(config.session));
io.on('connection', function (socket) {
socket.handshake.session.data = "whatever data I want";
socket.handshake.session.save();
});
这篇关于Socket.io 1.0.5:如何保存会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!