简体   繁体   English

Socket.io Express 3会话

[英]Socket.io Express 3 sessions

Is there a good way of using sessions with Socket.io in Express 3.0? 有没有一种在Express 3.0中使用Socket.io会话的好方法? A way of getting the clients' session id in a safe way? 一种以安全的方式获取客户会话ID的方法? So that I can send notices to members specific to their account and make private chats from member to member? 这样我就可以向会员特定的会员发送通知,并从会员到会员进行私聊?

I'm using MySQL store in Express 3.0 我在Express 3.0中使用MySQL商店

I wrote a tiny module to abstract it, here's how its usage looks like. 我写了一个小模块来抽象它,这是它的用法看起来如何。 It was written and tested using express 3, socket.io 0.9.10 and the (default) MemoryStore from connect 2.4.5. 它是使用express 3,socket.io 0.9.10和来自connect 2.4.5的(默认)MemoryStore编写和测试的。 It should work fine with other compatible stores. 它应该与其他兼容商店一起使用。

var SessionSockets = require('session.socket.io')
  , sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

sessionSockets.on('connection', function (err, socket, session) {
  //your regular socket.io code goes here
});

For more details on how it works see https://github.com/wcamarao/session.socket.io 有关其工作原理的更多详细信息,请参阅https://github.com/wcamarao/session.socket.io

You might want to pay attention to the part of the README where it says how to use it with your own session store key (I'm assuming your mysql store uses a name other than the default 'connect.sid'). 您可能需要注意README的部分,它说明如何将它与您自己的会话存储密钥一起使用(我假设您的mysql存储使用的名称不是默认的'connect.sid')。

You should check out express.io , a very simple micro-framework for express and socket.io integration. 你应该看一下express.io ,一个非常简单的用于express和socket.io集成的微框架。 It handles express and socket.io sessions automatically. 它自动处理express和socket.io会话。

npm install express.io

Check out the session support example here: 查看会话支持示例:

https://github.com/techpines/express.io/tree/master/examples#sessions https://github.com/techpines/express.io/tree/master/examples#sessions

I did something slightly different to get it working. 我做了一些稍微不同的工作。 I read through a lot of posts on nodester github and came with the following solution.... 在nodester github上阅读了很多帖子并提供了以下解决方案....

Replace: 更换:

http.createServer(app).listen(app.get('port'), function(){
      console.log("Express server listening on port " + app.get('port'));
    });

with: 有:

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

var io = require('socket.io').listen(server);

My plan is to continue with this workaround until issues around express3 and socket.io are resolved. 我的计划是继续这个解决方法,直到解决了express3和socket.io的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM