在nodeJS中获取SESSIONID [英] Get SESSIONID in nodeJS
问题描述
现在,在玩了几个小时的nodejs和socket.io之后,我遇到了另外两个问题-一个问题是,我需要在node.js内获取sessionID,然后使用app.get('/' ...
退出-因为连接socket.io
时似乎不会触发该事件,它只会触发.on('connection', function( ...
Now, after some hours of playing around with nodejs and socket.io, I'm getting a couple more problems - one being, that I need to get the sessionID inside node.js, whitout using app.get('/' ...
- since that event doesnt seem to fire when socket.io
connects, it only fires .on('connection', function( ...
var express = require('express')()
express.set('port', process.env.PORT || 8080)
var server = require('http').createServer(express)
var socket = require('socket.io').listen(server)
server.listen(express.get('port'))
// this event is fired, get('/', ... isnt!
server.on('connection', function(stream) {
// ??
} )
Session最初是由PHP应用程序创建的,当用户登录时.Session数据存储在数据库中,我需要访问该数据的键是SESSION ID.最简单的方法是什么?如前所述,我找到了几个使用app.get('/' ...
的示例,但我根本无法触发该事件.
The Session is initially created by the PHP application, as the user logs in. Session data is stored in the database, and the key I need to access that data is the SESSION ID. What's the easiest way to get to it? Like mentioned, I found a couple examples that used app.get('/' ...
but I couldnt get that event to fire at all.
谢谢.
推荐答案
如果会话数据被存储为cookie(最有可能),那么您应该能够在套接字握手期间重新解析该数据.我在此答案上发布了该代码,但也在此处复制了该代码:
If the session data is being stored as a cookie (most likely), then you should be able to re-parse that data during the socket handshake. I posted code for that on this answer, but copied the code here as well:
io.configure(function () {
io.set('authorization', function (handshakeData, callback) {
var cookie = handshakeData.headers.cookie;
// parse the cookie to get user data...
// second argument to the callback decides whether to authorize the client
callback(null, true);
});
});
如果会话数据正在URL中传播,则可以从handshakeData.url
或handshakeData.query
收集此信息.您可能必须要做自己的实验.
If the session data is being propagated in the URL, then you may be able to gather this information from handshakeData.url
or handshakeData.query
. You'll probably have to do your own experimentation.
这篇关于在nodeJS中获取SESSIONID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!