简体   繁体   English

集成socket.io-Express 4,相同的端口和安全会话

[英]integration socket.io - express 4 ,same port and security session

I'm working with mean.js and I need to have some real time features in my app, to accomplish that I'm going to use socket.io library. 我正在使用mean.js,并且我的应用程序中需要具有一些实时功能,才能使用socket.io库。 Here is my idea on how to integrate and still have a good structure in the app. 这是我关于如何集成应用程序并保持其良好结构的想法。

Mean is using a server.js file, that is the one that do a lot of configurations, so I want to do the following: 意思是使用server.js文件,该文件做了很多配置,所以我要执行以下操作:

// Expose app
exports = module.exports = app;

// Add my reference to the socketServer
var io = require('/socketServer')(app);

The file '/socketServer.js' is going to be my starting point and my configuration point of my socket, could looks something like this: 文件“ /socketServer.js”将是我的起点,而我的套接字的配置点可能看起来像这样:

var http = require('http');
var socketio = require('socket.io');

module.exports = function(express){
    var server = http.Server(express);
    var io = socketio(server);
    io.path('/');
    io.on('connect', function(socket){
        socket.emit('connected', {msg: 'You are connected now.'});
        socket.on('upvote', function(data){

            socket.emit('upvoteR', 'newConnected');
            socket.broadcast.emit('upvoteR', 'newCOnnected');
        });
    });
    server.listen(8080);
    return io;
};

I feel like could be useful for me separate the server default config, of my socket config, and use it file (socketServer.js) as my starting point to develop all my sockets logics injecting the dependencies I want. 我觉得将服务器的默认配置与套接字配置分开,并以它的文件(socketServer.js)作为起点来开发所有套接字逻辑以注入所需的依赖关系可能对我很有用。 I don't know if is out there a better approach to this problem, or some structure best practices that I should follow or inconveniences of doing this. 我不知道是否有解决此问题的更好方法,或者我应该遵循的一些结构最佳实践或这样做的不便之处。

So besides this structure, this are other doubts: 因此,除了这种结构之外,还有其他疑问:

  • How to use sockets and express server in the same port? 如何在同一端口使用套接字和Express服务器? Seems like, with express 4 I'm not able to link the express server with socket, because express 4 server does not inherit any more of httpServer of node.js, so now I have to do a server.listen(socketPort) and if I use the same app.port of mean.js this just is an EADDRINUSE error. 似乎,对于Express 4,我无法将Express服务器与套接字链接,因为Express 4服务器不再继承node.js的httpServer,所以现在我必须做一个server.listen(socketPort),如果我使用与mean.js相同的app.port,这只是一个EADDRINUSE错误。 Is still possible to have it working in the same port ? 仍然可以在同一端口上工作吗?
  • How to use express session to authenticate each socket connection? 如何使用快速会话来验证每个套接字连接? if not possible, what's the better approach ? 如果不可能,有什么更好的方法? An example or a document reference would be nice for me. 一个例子或文档参考对我来说是很好的。

thanks in advance. 提前致谢。

I would like to share my solution just in case someone in the future has the same requirement that I had. 我希望分享我的解决方案,以防将来有人遇到与我相同的要求。

How to authenticate each socket connection base on express session information. 如何基于快速会话信息对每个套接字连接进行身份验证。

First I configure express to use passport.js library the following way: 首先,我将express配置为通过以下方式使用passport.js库:

// CookieParser should be above session
    var cp =cookieParser;
    app.use(cp());

    // Express MongoDB session storage
    var mStore = new mongoStore({
            db: db.connection.db,
            collection: config.sessionCollection
        });

    app.use(session({
        secret: config.sessionSecret,
        store: mStore
    }));

    // use passport session
    app.use(passport.initialize());
    app.use(passport.session());

So far is the normal implementation of passport over express. 到目前为止,护照过快通行的正常实施。 be sides this configuration I added passport-socket.io.js to my project. 除了这种配置,我在我的项目中添加了password-socket.io.js This is my working configuration: 这是我的工作配置:

var server = http.Server(app);
    var io = IO(server);

    io.use(
        function(socket,next){
            passportSocketIo.authorize({
                cookieParser: cp,
                key:         'connect.sid',             // the name of the express cookie 
                secret:      config.sessionSecret,      // the session_secret to parse the cookie
                store:       mStore,                    // mongo session storage
                success:     onAuthorizeSuccess,        // *optional* callback on success
                fail:        onAuthorizeFail,           // *optional* callback on fail/error
            })(socket, next);
        }
    );
    app.io=io;
    server.listen(config.port);

Where "onAuthorizeSuccess" and "onAuthorizeFail" are functions to allow the conections and develop the sockets logics.. well,with this my socket.io connection is authenticated with my passport session information and if the user is not logged the socket would not connect.. 其中“ onAuthorizeSuccess”和“ onAuthorizeFail”是允许连接和开发套接字逻辑的函数。通过我的socket.io连接可以用我的护照会话信息进行身份验证,如果用户未登录,套接字将无法连接。 。

And if we need some authorization logic based on user roles, the passport.socketio creates a socket.request.user where you can find yours users roles to use in your roles sockets logics.. 并且,如果我们需要基于用户角色的一些授权逻辑,那么passport.socketio将创建一个socket.request.user ,您可以在其中找到要在您的角色套接字逻辑中使用的用户角色。

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

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