本文介绍了socket.io和session?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用express框架。我想从socket.io访问会话数据。我尝试用client.listener.server.dynamicViewHelpers数据表达dynamicHelpers,但是我无法获取会话数据。有没有一个简单的方法来做到这一点?请看代码

  app.listen(3000); 

var io = require('socket.io');
var io = io.listen(app);

io.on('connection',function(client){
//我想在这里使用会话数据
client.on('message',function(message) {
//或这里
});
client.on('disconnect',function(){
//或这里
});
});


解决方案

这不适用于跨越flashsocket的套接字运输(它不发送服务器所需的cookie),但它可靠地为其他所有工作。为了使其工作,在express / connect端,我明确定义会话存储,所以我可以在socket中使用它。 :

  MemoryStore = require('connect / middleware / session / memory'),
var session_store = new MemoryStore );
app.configure(function(){
app.use(express.session({store:session_store}));
});

然后在我的套接字代码中,我包含了连接框架,所以我可以使用它的cookie解析来检索connect.sid从cookie。然后,我在会话存储中查找具有该connect.sid的会话,如下所示:

  var connect = require('connect ); 
io.on('connection',function(socket_client){
var cookie_string = socket_client.request.headers.cookie;
var parsed_cookies = connect.utils.parseCookie(cookie_string);
var connect_sid = parsed_cookies ['connect.sid'];
if(connect_sid){
session_store.get(connect_sid,function(error,session){
// HOORAY NOW YOU VE GOT THE SESSION OBJECT !!!!
});
}
});

然后,您可以根据需要使用会话。


I'm using express framework. I want to reach session data from socket.io. I tried express dynamicHelpers with client.listener.server.dynamicViewHelpers data, but i can't get session data. Is there a simple way to do this? Please see the code

app.listen(3000);

var io = require('socket.io');
var io = io.listen(app);

io.on('connection', function(client){
    // I want to use session data here
    client.on('message', function(message){
        // or here
    });
    client.on('disconnect', function(){
        // or here
    });
});
解决方案

This won't work for sockets going over the flashsocket transport (it doesn't send the server the needed cookies) but it reliably works for everything else. I just disable the flashsocket transport in my code.

To make it work, in the express/connect side, I explicitly define the session store so I can use it inside socket:

MemoryStore = require('connect/middleware/session/memory'),
var session_store = new MemoryStore();
app.configure(function () {
  app.use(express.session({ store: session_store }));
});

Then inside my socket code, I include the connect framework so I can use its cookie parsing to retrieve the connect.sid from the cookies. I then look up the session in the session store that has that connect.sid like so:

var connect = require('connect');
io.on('connection', function(socket_client) {
  var cookie_string = socket_client.request.headers.cookie;
  var parsed_cookies = connect.utils.parseCookie(cookie_string);
  var connect_sid = parsed_cookies['connect.sid'];
  if (connect_sid) {
    session_store.get(connect_sid, function (error, session) {
      //HOORAY NOW YOU'VE GOT THE SESSION OBJECT!!!!
    });
  }
});

You can then use the session as needed.

这篇关于socket.io和session?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:01