Socket.io命名空间和服务器不共享中间件?

时间:2022-04-20 19:42:57

I'm created some passport authentication middleware for my socket.io/express app. It looks like:

我为socket.io/express app创建了一些护照认证中间件。看起来像:

io.use(function (socket, next) {
    var data = cookie.parse(socket.handshake.headers.cookie);
    var sessionID = cookieParser.signedCookie(data['connect.sid'], 'my balonga has a first name');
    sessionStore.get(sessionID, function (err, data) {
        if (err) next(err);
        socket.handshake.passport = data.passport;
        next();
    });
});

It works great, but I have a namespace and it appears uses a different socket. Does this mean that I must reuse my middleware for each namespace?

它工作得很好,但我有一个命名空间,它似乎使用不同的套接字。这是否意味着我必须为每个命名空间重用我的中间件?

I noticed that I connect to my namespace it calls the middleware for the base server and then the namespace, this means if I include the middleware in both places, I'm doing 2x the operations I need. Can I prevent this without removing the middleware at the base layer? None of these are app breakers, but it will change my architecture a bit and I'm concerned I'll have auth gaps at some point.

我注意到我连接到我的命名空间它调用基本服务器的中间件然后调用命名空间,这意味着如果我在两个地方都包含中间件,我正在做我需要的操作的2倍。如果不删除基础层的中间件,我可以防止这种情况吗?这些都不是应用程序破坏者,但它会改变我的架构,我担心在某些时候我会有auth间隙。

Summary:

概要:

  • Must I reuse my middleware for each namespace?
  • 我必须为每个命名空间重用我的中间件吗?
  • Can I prevent the default namespace middleware from being called without removing the middleware at the base layer, when it is a namespace being connected to?
  • 我是否可以阻止在不删除基础层的中间件的情况下调用默认命名空间中间件,当它是连接到的命名空间时?

1 个解决方案

#1


0  

Must I reuse my middleware for each namespace?

No. If you plan on using the middleware, as written, for all namespaces, you can utilize io.use() like you are currently doing. This places the middleware on the default / namespace. As you have already noted, the middleware on all sub-namespaces fire off in order, starting with the default, on a new socket.io connection.

不可以。如果您计划使用所有名称空间的中间件,您可以像目前一样使用io.use()。这将中间件放在默认/命名空间中。正如您已经注意到的那样,所有子命名空间上的中间件按顺序从新的socket.io连接上的默认值开始。

Can I prevent the default namespace middleware from being called without removing the middleware at the base layer, when it is a namespace being connected to?

No. All namespaces are a subspace of the default namespace and any middleware defined on the default namespace, will fire for all connections. If you wanted to only fire your middleware off on certain namespaces, you could refrain from using io.use() and do the following.

不可以。所有名称空间都是默认名称空间的子空间,默认名称空间中定义的任何中间件都将为所有连接触发。如果您只想在某些名称空间上关闭中间件,则可以避免使用io.use()并执行以下操作。

const myMiddleware = function(socket, next) {
    // Do stuff here
};
io.of('/nsp_1').use(myMiddleware);
io.of('/nsp_2').use(myMiddleware);

A More Modular Approach

A more modular way to do this is to put the handlers for different namespaces in different different files, and define middleware specific for those namespaces in those files, while using io.use() for the default namespace.

更加模块化的方法是将不同命名空间的处理程序放在不同的不同文件中,并为这些文件中的那些命名空间定义特定的中间件,同时使用io.use()作为默认命名空间。

main_app.js

main_app.js

const everthingMiddleware = require('./middleware/everything.js');
io.use(everythingMiddleware);
require('./nsp_1.js')(io.of('/nsp_1'));

nsp_1.js

nsp_1.js

module.exports = function(io) {
    const onePlaceMiddleware = require('./middleware/oneplace.js');
    io.use(onePlaceMiddleware);
};

#1


0  

Must I reuse my middleware for each namespace?

No. If you plan on using the middleware, as written, for all namespaces, you can utilize io.use() like you are currently doing. This places the middleware on the default / namespace. As you have already noted, the middleware on all sub-namespaces fire off in order, starting with the default, on a new socket.io connection.

不可以。如果您计划使用所有名称空间的中间件,您可以像目前一样使用io.use()。这将中间件放在默认/命名空间中。正如您已经注意到的那样,所有子命名空间上的中间件按顺序从新的socket.io连接上的默认值开始。

Can I prevent the default namespace middleware from being called without removing the middleware at the base layer, when it is a namespace being connected to?

No. All namespaces are a subspace of the default namespace and any middleware defined on the default namespace, will fire for all connections. If you wanted to only fire your middleware off on certain namespaces, you could refrain from using io.use() and do the following.

不可以。所有名称空间都是默认名称空间的子空间,默认名称空间中定义的任何中间件都将为所有连接触发。如果您只想在某些名称空间上关闭中间件,则可以避免使用io.use()并执行以下操作。

const myMiddleware = function(socket, next) {
    // Do stuff here
};
io.of('/nsp_1').use(myMiddleware);
io.of('/nsp_2').use(myMiddleware);

A More Modular Approach

A more modular way to do this is to put the handlers for different namespaces in different different files, and define middleware specific for those namespaces in those files, while using io.use() for the default namespace.

更加模块化的方法是将不同命名空间的处理程序放在不同的不同文件中,并为这些文件中的那些命名空间定义特定的中间件,同时使用io.use()作为默认命名空间。

main_app.js

main_app.js

const everthingMiddleware = require('./middleware/everything.js');
io.use(everythingMiddleware);
require('./nsp_1.js')(io.of('/nsp_1'));

nsp_1.js

nsp_1.js

module.exports = function(io) {
    const onePlaceMiddleware = require('./middleware/oneplace.js');
    io.use(onePlaceMiddleware);
};