How to add parameters to a FeathersJS socket connection

这一生的挚爱 提交于 2020-01-16 17:07:29

问题


I'm developing an app that uses a FeathersJS server, and an Electron app that uses FeathersJS Socket.io client to connect to the server. I'd like to use channels in the Electron app to get notified when some data in the server has changed. The Electron app is an "autonomuos" app, so there isn't any user authentication or interaction. The Electron app will have a unique id that represents each machine it is running on.

Taking into account that there isn't any kind of authentication, how can I add the Electron app's unique id in the socket.io connection so that this id is received in the server and it can create the corresponding channel in the FeathersJS server?

Thank you!


回答1:


How to pass information on the server from the Socket.io transport to Feathers through a 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. Put together it looks like this on the client:

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

And like this on the server:

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

socket.feathers is the exact same as the channel connection object so it will now have a token property you can use to join as a channel:

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


来源:https://stackoverflow.com/questions/54421486/how-to-add-parameters-to-a-feathersjs-socket-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!