Socket.io通过XHR轮询不通过Cookie

我试图将Express(3.x)与NowJS(主要是围绕着Socket.io的封装)结合起来创build一个实时应用程序,首先需要你login。我确实是Node的新手,但是我有一个很好的处理每个包如何独立工作。 但是,从NowJS访问快速会话信息是给我适合的。

这个问题似乎是因为我不得不使用XHR-Polling(使用Heroku作为主机平台),所以我无法轻松访问Socket.io身份validation方法中的cookie信息。 在authFunction下面的代码中,“data.headers.cookie”是未定义的。 以一种迂回的方式(在Now.js代码中),这会在nowjs.on(“connect”,…)callback中留下“this.user.cookie”为空(但不是未定义的)。 这反过来让我无法抓住会议信息。

从阅读来看,似乎cookie只有在使用websocket时才可用。 没有cookie,我不知道哪个用户正在调用服务器,除了为它们生成的随机标识符。

任何人都可以build议如何处理这个? 在build立与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); }); 

编辑:当然,我会喜欢简单的解决scheme,像这里顶部的答案: 在Now.js中的会话支持 ,但这不适用于我(可能)同样的原因 – 我的“cookie”是空的,不包含“连接.sid“键。

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

在使用Cloud9进行debugging时,他们为您提供了一个很好的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 / …”作为我的debugging基础,而且它似乎正常工作100%。 Heroku似乎没有这个问题,因为所有请求(常规页面请求 socketio)都是通过同一个主机。