节点无法获取会话ID

时间:2016-12-16 13:19:22

标签: php node.js memcached

这是Node.js异步性质的问题吗?我有一段代码通过Memcache获取PHP会话,如下所示:

var SessionID, Session;
IO.sockets.on('connection', function (client) {
    SessionID = client.handshake.headers.cookie;
    Session = GetSession(SessionID);
    console.log(Session); // here, Session is all the undefined values
});

然后GetSession()函数如下所示:

function GetSession(SessionID) {
    var Session = [];
    // Define a default value for all Session values so undefined warnings aren't thrown for non-members
    Session = [];
    Session['MemberID'] = 0;
    Session['MemberEmail'] = undefined;
    Session['MemberPass'] = undefined;

    // Make sure SessionID has been stripped down to just PHP Session ID
    if ( SessionID.split(';').length > 1 ) {
        var parts = SessionID.split(';');
        var vals = [];
        var thisPart, subParts;
        for(var k = 0; k < parts.length; k++) {
            thisPart = parts[k];
            if ( thisPart.indexOf('PHPSESSID') >= 0 ) {
                subParts = thisPart.split('=');
                if ( subParts.length > 1 )
                    SessionID = subParts[1];
            }
        }
    }

    // Get session vars from Memcache
    MemCache.get('Session-'+SessionID, function(err,result){
        Session = JSON.parse(result);
        console.log(Session); // here, Session prints the correct values
    });

    return Session;
}

辩论将我依赖Session的所有代码扔到传递给GetSession()函数的回调函数中,但这似乎不必要地混乱。

1 个答案:

答案 0 :(得分:0)

是的,你在异步函数中遇到问题。

您需要向GetSession添加回调。

var SessionID, Session;
IO.sockets.on('connection', function (client) {
  SessionID = client.handshake.headers.cookie;
  Session = GetSession(SessionID, function (error, result) {
    console.log(result); // here, Session
  });

});


function GetSession(SessionID, cb) {
  var Session = [];
  // Define a default value for all Session values so undefined warnings aren't thrown for non-members
  Session = [];
  Session['MemberID'] = 0;
  Session['MemberEmail'] = undefined;
  Session['MemberPass'] = undefined;

  // Make sure SessionID has been stripped down to just PHP Session ID
  if ( SessionID.split(';').length > 1 ) {
    var parts = SessionID.split(';');
    var vals = [];
    var thisPart, subParts;
    for(var k = 0; k < parts.length; k++) {
      thisPart = parts[k];
      if ( thisPart.indexOf('PHPSESSID') >= 0 ) {
        subParts = thisPart.split('=');
        if ( subParts.length > 1 )
          SessionID = subParts[1];
      }
    }
  }

  // Get session vars from Memcache
  MemCache.get('Session-'+SessionID, function(err,result){
    if(err){
        return cb(err);
    }
    Session = JSON.parse(result);
    console.log(Session);
    return cb(null, Session);//First arg for Error
  });

}

或承诺承诺

&#13;
&#13;
var SessionID, Session;
IO.sockets.on('connection', function (client) {
    SessionID = client.handshake.headers.cookie;

    GetSession(SessionID)
        .then(function (result) {
            Session = result;
            console.log(result); // here, Session is all the undefined values
        })
        .catch(function (error) {
            console.log(error);
        });
});


function GetSession(SessionID) {
    return new Promise(function (resolve, reject) {
        var Session = [];
        // Define a default value for all Session values so undefined warnings aren't thrown for non-members
        Session = [];
        Session['MemberID'] = 0;
        Session['MemberEmail'] = undefined;
        Session['MemberPass'] = undefined;

        // Make sure SessionID has been stripped down to just PHP Session ID
        if (SessionID.split(';').length > 1) {
            var parts = SessionID.split(';');
            var vals = [];
            var thisPart, subParts;
            for (var k = 0; k < parts.length; k++) {
                thisPart = parts[k];
                if (thisPart.indexOf('PHPSESSID') >= 0) {
                    subParts = thisPart.split('=');
                    if (subParts.length > 1)
                        SessionID = subParts[1];
                }
            }
        }

        // Get session vars from Memcache
        MemCache.get('Session-' + SessionID, function (err, result) {
            if(err){
                return reject(error);
            }
            Session = JSON.parse(result);
            console.log(Session); // here, Session prints the correct values
            return resolve(Session);
        });
    });
}
&#13;
&#13;
&#13;