如何将参数添加到 FeathersJS 套接字连接

How to add parameters to a FeathersJS socket connection

我正在开发一个使用 FeathersJS 服务器的应用程序,以及一个使用 FeathersJS Socket.io 客户端连接到服务器的 Electron 应用程序。我想在 Electron 应用程序中使用通道,以便在服务器中的某些数据发生更改时得到通知。 Electron 应用程序是一个 "autonomuos" 应用程序,因此没有任何用户身份验证或交互。 Electron 应用程序将有一个唯一的 ID,代表它 运行 所在的每台机器。

考虑到没有任何类型的身份验证,我如何在 socket.io 连接中添加 Electron 应用程序的唯一 ID,以便服务器接收到此 ID 并创建相应的FeathersJS 服务器中的频道?

谢谢!

如何通过 Socket.io middleware is documented in the Socket.io transport API. There also is an example how to pass query parameters from the client to the server in the Socket.io documentation 将服务器上的信息从 Socket.io 传输到 Feathers。在客户端上放在一起看起来像这样:

const socket = io('http://feathers-server.com', {
  query: {
    token: 'cde'
  }
});

在服务器上像这样:

app.configure(socketio(function(io) {
  io.use(function (socket, next) {
    socket.feathers.token = socket.handshake.query.token;
    next();
  });
}));

socket.feathers 与频道 connection 对象完全相同,因此它现在将有一个 token 属性,您可以使用它作为频道加入:

app.on('connection', connection => {
  if (connection.token) {
    app.channel(connection.token).join(connection);
  }
});