节点/套接字/快速 - 试图将所有内容放在一起的错误 - 会话未定义

时间:2013-11-24 16:29:25

标签: node.js sockets session

我正在尝试从socket中设置会话对象的值,这样做我发现我的配置不正确,我不知道为什么。

这是我所有的相关代码,我显然在那里做了一些错误的~100行:

var express = require('express')
var engine = require('ejs-locals')
var app = express()
  , cookieParser = express.cookieParser('secret')
  , server = require('http').createServer(app)
  , cookie  =   require('cookie')
  , connect =   require('connect')
  , MemoryStore = connect.middleware.session.MemoryStore
  , sessionStore
  , io = require('socket.io').listen(server);
io.set('log level', 2);

app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(cookieParser);
//app.use(express.session({ secret:'secret', key:'express.sid', store:sessionStore = new   MemoryStore() }));
app.use(express.session({ store:sessionStore = new MemoryStore() }));

io.set('authorization', function (data, accept) {
  if (!data.headers.cookie) 
    return accept('No cookie transmitted.', false);

  data.cookie = cookie.parse(data.headers.cookie);
  data.sessionID = data.cookie['express.sid'];
  console.log(data.sessionID);
  console.log(sessionStore);
  sessionStore.get(data.sessionID, function (err, session) {
    if (err)
      accept(err.message, false); //Turn down the connection
    else {
      console.log(session);
      data.session = session; //Accept the session
      accept(null, true);
    }
  });
});

var mysql = require('mysql');
var dbConnection = mysql.createConnection({
  //connection string
});

app.engine('ejs', engine); // use ejs-locals for all ejs templates:
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs'); // so you can render('index')

dbConnection.connect(function(error) {
  if (error) {
    return console.log("CONNECTION error: " + error);
  }
});

server.listen(8080); //4000


app.get('/lobby/:id', requireAuth, function(request, response){
  console.log('in lobby load');
  response.render('lobby', { player: request.session.player, error: '', room: request.params.id});
});

app.get('/', function(request,response){
  response.render('index', { player: request.session.player , error: '', room: ''});
});

app.post('/', function(request, response){ //login
  console.log('booyeah1');
  var post = request.body;
  dbConnection.query("SELECT id, username, rating, pref_cut_deal, pref_cut_starter, pref_play_card, pref_count_hands FROM players WHERE username = '"+ post.username +"' and password = SHA1('"+ post.password +"') ORDER BY username LIMIT 1",
  function (error, results, fields) {  
    if (error) {
      response.render('index', { player: request.session.player , error: '', room: ''});
      console.log(error);
    }
    if (results.length  > 0) {
      dbConnection.query("Insert into playersessions (player_id, browser_os) values ("+ results[0]['id'] +", '"+ post.browser_os +"' );");
      request.session.player = { id: results[0]['id'], userName: results[0]['username'], rating: results[0]['rating'], prefCutDeal: results[0]['pref_cut_deal'], prefCutStarter: results[0]['pref_cut_starter'], prefPlayCard: results[0]['pref_play_card'], prefCountHands: results[0]['pref_count_hands']};
      response.redirect('/lobby/a');
      console.log('booyeah2');
    }
    else {
      response.render('index', { player: request.session.player , error: 'incorrect username/password', room: ''});
    }
  });
});

function requireAuth(request, response, next) {
  if (!request.session.player)
    response.redirect('/');
  else
    next();
}

var SessionSockets = require('session.socket.io'), 
sessionSockets = new SessionSockets(io, sessionStore, cookieParser);
sessionSockets.on('connection', function (err, socket, session, client) { clientConnect(err, socket, session,client) });

function clientConnect(err, socket, session, client) {
  console.log(session);
  if (session == null) {
    socket.emit('sign out');
  }
  else {
    socket.on('join room', function (room) {
      //do stuff 
    });
  }
}

当我运行上述内容时,一切正常。除了cookie中的会话ID和sessionStore中的会话ID不匹配。但是,当我评论出来时:
    app.use(express.session({store:sessionStore = new MemoryStore()}));
我评论说:
    //app.use(express.session({secret:'secret',key:'express.sid',store:sessionStore = new MemoryStore()}));
这是我认为我需要的会话ID彼此一致,然后在clientConnect中会话为空并且我在登录时立即注销。任何想法我哪里出错?

2 个答案:

答案 0 :(得分:0)

尝试以下操作,如果有效,请告诉我。

io.set('authorization', function (data, accept) {
  if (!data.headers.cookie) 
    return accept('No cookie transmitted.', false);

  data.cookie = cookie.parse(data.headers.cookie);
  cookieParser(data, {}, function(err) {
    if(data.signedCookies){
      data.sessionID = data.signedCookies['express.sid']; //fixed typo here
      console.log(data.sessionID);
      console.log(sessionStore);
      sessionStore.get(data.sessionID, function (err, session) {
        if (err)
          accept(err.message, false); //Turn down the connection
        else {
          console.log(session);
          data.session = session; //Accept the session
          accept(null, true);
        }
      });
    }
  });
});

答案 1 :(得分:0)

所以我开始工作了。我只是在破解我不想要的快速sid部分。我不确定是否有更优雅/防故障的方法来做到这一点?然后我使用sessionStore.load而不是.get。

io.set('authorization', function (handshake, accept) {
  if (!handshake.headers.cookie) 
    return accept('No cookie transmitted.', false);
  handshake.cookie = cookie.parse(handshake.headers.cookie);
  handshake.sessionID = handshake.cookie['express.sid'].substring(2).split('.')[0];
  sessionStore.load(handshake.sessionID, function (err, session) {
    if (err)
      accept(err.message, false); //Turn down the connection
    else {
      handshake.session = session; //Accept the session
      accept(null, true);
    }
  });
});