socket.io 的可选授权

optional autetification for socket.io

我使用 socket.io-jwt 库,代码如下:

socketio.use(require('socketio-jwt').authorize({
  secret: config.secrets.session,
  handshake: true
}));

但这限制了未经身份验证。我需要身份验证是可选的,并且可以在您登录后检查。

有图书馆吗?

我通过将失败选项传递给授权函数解决了这个问题。您应该调用 accept(null) 以声明没有错误。

socketio.use(require('socketio-jwt').authorize({
    secret: config.secrets.session,
    handshake: true,
    fail: function (error, data, accept) {
      if (data.request) {
        accept(null);
      } else {
        accept(null, false);
      }
    }
  }));