3

我正在尝试将 Express (3.x) 与 NowJS(主要是 Socket.io 的包装器)结合起来创建一个实时应用程序,首先需要您登录。我承认我是 Node 新手,但我有很好地处理每个包如何单独工作。但是,从 NowJS 访问 Express Session 信息让我很适应。

问题似乎是因为我被迫使用 XHR-Polling(使用 Heroku 作为托管平台),我无法轻松访问 Socket.io 身份验证方法中的 cookie 信息。在下面的 authFunction 代码中,“data.headers.cookie”是未定义的。以一种迂回的方式(在 Now.js 代码中),这在 nowjs.on("connect",...) 回调中将“this.user.cookie”留空(但不是未定义)。这反过来又让我无法获取会话信息。

通过阅读,似乎只有使用 websockets 时 cookie 才可用。如果没有 cookie,除了为他们生成的随机标识符之外,我不知道哪个用户正在调用服务器。

谁能建议如何解决这个问题?在与 Now.js 服务器建立连接后,我是否需要从客户端手动发送 cookie?

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.engine('html', consolidate.swig);
  app.set('view engine', 'html');

  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ secret: "secrethere" , store: sessionStore}));

  app.use(express.static(path.join(__dirname, 'public')));
  app.use(app.router);
});

app.configure('development', function(){
  app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
});

//Map the routes
var routes = require('./routes/routes')(db);
app.get[...] //routes defined here

var server = http.createServer(app, {cookieKey: 'connect.sid'});
server.listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

var parseCookie = require('express/node_modules/connect').utils.parseCookie;
var authFunction = function (data, accept) {
// check if there's a cookie header
if (data.headers.cookie) {
    // if there is, parse the cookie
    data.cookie = parseCookie(data.headers.cookie);

    // note that you will need to use the same key to grad the
    // session id, as you specified in the Express setup.
    data.sessionID = data.cookie['connect.sid'];
} else {
   // if there isn't, turn down the connection with a message
   // and leave the function.
   return accept('No cookie transmitted.', false);
}

// accept the incoming connection
accept(null, true);
};

var everyone = nowjs.initialize(server, {socketio: {transports: ['xhr-polling', 'jsonp-polling'], authorization: authFunction}});

nowjs.on('connect', function (socket) {
//TODO - This will need to be based on the URL that is being visited, or
//similar to establish that users are viewing the same thing
var sid = unescape(this.user.cookie["connect.sid"]); //# you DO have to unescape the session id!

sessionStore.get(sid, function(err, sess){
    console.log("That group who joined? his userId is " + sess.userId)
});

var newRoom = "group";//this.user.session;//.groupName;
var newGroup = nowjs.getGroup(newRoom);
this.now.serverRoom = newRoom;
newGroup.addUser(this.user.clientId);
});

编辑:我当然更喜欢简单的解决方案,比如这里的最佳答案:Now.js 中的会话支持,但这对我不起作用(可能)出于同样的原因——我的“cookie”是空的并且不包含“连接” .sid”键。

4

1 回答 1

0

经过大量尝试不同的事情后,我意识到这实际上与我使用的环境(Cloud9 IDE)有关。

使用 Cloud9 进行调试时,它们会为您提供一个不错的 url,让您在“http://[projectname].[username].c9.io”处浏览您的站点。但是,NowJS 将其请求发送到“http://project-[unique ID].rhcloud.com/socket.io/1/xhr-polling/...”。这并没有提示 cookie 与请求一起发送,因为它与 cookie 关联的主机不同(好吧,这里的术语可能有点偏离)。

为了解决这个问题,我使用“http://project-[unique ID].rhcloud.com/...”作为调试的基础,它似乎 100% 正常工作。Heroku 似乎没有这个问题,因为所有请求(常规页面请求socketio)都通过同一个主机。

于 2012-11-23T19:13:10.427 回答