socket.io 和会话?
我正在使用快速框架。我想从 socket.io 获取会话数据。我尝试使用 client.listener.server.dynamicViewHelpers 数据表达dynamicHelpers,但我无法获取会话数据。有没有一种简单的方法可以做到这一点?请看代码
app.listen(3000);
var io = require('socket.io');
var io = io.listen(app);
io.on('connection', function(client){
// I want to use session data here
client.on('message', function(message){
// or here
});
client.on('disconnect', function(){
// or here
});
});
I'm using express framework. I want to reach session data from socket.io. I tried express dynamicHelpers with client.listener.server.dynamicViewHelpers data, but i can't get session data. Is there a simple way to do this? Please see the code
app.listen(3000);
var io = require('socket.io');
var io = io.listen(app);
io.on('connection', function(client){
// I want to use session data here
client.on('message', function(message){
// or here
});
client.on('disconnect', function(){
// or here
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这不适用于通过 flashsocket 传输的套接字(它不会向服务器发送所需的 cookie),但它可以可靠地适用于其他所有内容。我只是在代码中禁用 flashsocket 传输。
为了使其工作,在express/connect方面,我显式定义了会话存储,以便我可以在套接字内使用它:
然后在我的套接字代码中,我包含连接框架,以便我可以使用它的cookie解析来检索connect.sid来自饼干。然后,我在会话存储中查找具有 connect.sid 的会话,如下所示:
然后,您可以根据需要使用该会话。
This won't work for sockets going over the flashsocket transport (it doesn't send the server the needed cookies) but it reliably works for everything else. I just disable the flashsocket transport in my code.
To make it work, in the express/connect side, I explicitly define the session store so I can use it inside socket:
Then inside my socket code, I include the connect framework so I can use its cookie parsing to retrieve the connect.sid from the cookies. I then look up the session in the session store that has that connect.sid like so:
You can then use the session as needed.
Socket.IO-sessions 模块解决方案通过在客户端(脚本)级别公开会话 ID,使应用程序遭受 XSS 攻击。
请检查此解决方案(适用于 Socket.IO >= v0.7)。请参阅此处的文档。
The Socket.IO-sessions module solution exposes the app to XSS attacks by exposing the session ID at the client (scripting) level.
Check this solution instead (for Socket.IO >= v0.7). See docs here.
我建议不要完全重新发明轮子。你需要的工具已经是一个npm包。我想这就是你需要的: session.socket.io
我这几天正在使用它,我想它会很有帮助!将express-session链接到socket.io层将会有很多好处!
I suggest not to entirely reinvent the wheel. The tools you need is already an npm package.I think this is what you need: session.socket.io
I am using it in these days and it will be very helpful I guess!! Linking the express-session to the socket.io layer will have so many advantages!
编辑:在尝试了一些不起作用的模块之后,我实际上已经编写了自己的库来做到这一点。无耻插件:去看看 https://github.com/aviddiviner/Socket.IO-sessions 。为了历史目的,我将在下面留下我的旧帖子:
我非常巧妙地完成了这项工作,而不必按照上面的 pr0zac 解决方案绕过 flashsocket 传输。我还在 Socket.IO 中使用express。方法如下。
首先,将会话 ID 传递给视图:
然后在您的视图中,将其与 Socket.IO 客户端链接:
然后在服务器端 Socket.IO 侦听器中,选取它并读取/写入会话数据:
在我的案例中,我的
session_store
是 Redis,使用redis-connect
库。希望这对在谷歌搜索时找到这篇文章的人有所帮助(就像我所做的那样;)
Edit: After trying some modules that didn't work, I've actually gone and written my own library to do this. Shameless plug: go check it out at https://github.com/aviddiviner/Socket.IO-sessions. I'll leave my old post below for historical purposes:
I got this work quite neatly without having to bypass the flashsocket transport as per pr0zac's solution above. I am also using express with Socket.IO. Here's how.
First, pass the session ID to the view:
Then in your view, link it in with Socket.IO client-side:
Then in your server-side Socket.IO listener, pick it up and read/write the session data:
In my case, my
session_store
is Redis, using theredis-connect
library.Hope this helps someone who finds this post while searching Google (as I did ;)
请参阅: Socket.IO 身份验证
我建议不要通过
client 获取任何内容.request...
或client.listener...
因为它不直接附加到client
对象,并且始终指向最后登录的用户!See this: Socket.IO Authentication
I would suggest not fetching anything via
client.request...
orclient.listener...
as that is not directly attached to theclient
object and always point to the last logged in user!对于未来的读者 - 有一种优雅且简单的方法可以使用express-session 访问socket.io 内的会话。来自socket.io文档:
For future readers - There is an elegant and easy way to access the session inside socket.io with express-session. From socket.io documentation:
查看 Socket.IO-connect
连接 Socket.IO-node 周围的 WebSocket 中间件包装器
https://github.com/bnoguchi/Socket.IO-connect
这将允许您将 Socket.IO 请求推送到 Express/Connect 中间件堆栈,然后使用 Socket.IO 事件处理程序进行处理,从而使您能够访问会话、cookie 等。不过,我不确定它是否适用于所有 Socket.IO 的传输。
Check out Socket.IO-connect
Connect WebSocket Middleware Wrapper Around Socket.IO-node
https://github.com/bnoguchi/Socket.IO-connect
This will allow you to push the Socket.IO request(s) down the Express/Connect middleware stack before handling it with Socket.IO event handlers, giving you access to the session, cookies, and more. Although, I'm not sure that it works with all of Socket.IO's transports.
我不确定我做得是否正确。
https://github.com/LearnBoost/socket.io/wiki/Authorizing
通过握手数据,您可以访问 cookie。在 cookie 中,您可以获取 connect.sid,它是每个客户端的会话 ID。然后使用 connect.sid 从数据库获取会话数据(我假设您正在使用 RedisStore)
I am not sure that I am doing it right.
https://github.com/LearnBoost/socket.io/wiki/Authorizing
With the handshake data, you can access to the cookies. And in the cookies, you can grab connect.sid which is the session id for each client. And then use the connect.sid to get the session data from database (I am assuming you are using RedisStore)
您可以使用 express-socket.io-session 。
与 socket.io 共享基于 cookie 的快速会话中间件。与快递合作> 4.0.0和socket.io> 1.0.0 并且不会向后兼容。
为我工作!
You can make use of express-socket.io-session .
Share a cookie-based express-session middleware with socket.io. Works with express > 4.0.0 and socket.io > 1.0.0 and won't be backward compatible.
Worked for me!!
截至 2022 年 2 月,Socket.IO v4 支持此功能:
https: //socket.io/docs/v4/faq/#usage-with-express-session
As of February 2022 this is supported by Socket.IO v4:
https://socket.io/docs/v4/faq/#usage-with-express-session
您可以看看这个: https://github.com/bmeck/session-web-sockets
或者您可以使用:
希望这有帮助。
You can have a look at this: https://github.com/bmeck/session-web-sockets
or alternatively you can use:
Hope this helps.