创建通道时将活动连接添加到通道

时间:2019-12-17 23:09:05

标签: node.js socket.io feathersjs

我试图防止谁可以看到特定页面的事件,所以我确定并存储哪些用户可以访问特定页面的ID。在channels.js中,我可以通过迭代如下页面将用户添加到app.on('connection')中新的页面级通道:app.channel('page-'+ pageId).join(connection)< / p>

问题在于,直到刷新浏览器并重新连接后,用户才开始从该页面获取广播。

我要发生的事情是在创建页面时让允许用户的所有连接开始广播。是否可以在channels.js中做到这一点,或者我可以告诉它是谁开始广播以创建页面呢?

编辑以添加我尝试过的最后一件事。 “用户页面”是链接用户和页面的关联实体。

app.service('users-pages').on('created', userPage => {
    const { connections } = app.channel(app.channels).filter(connection =>
      connection.user.id === userPage.userId
    )
    app.channel('page-' + userPage.pageId).join(connections)
})

2 个答案:

答案 0 :(得分:0)

keeping channels updated文档应包含您要寻找的答案。它使用users服务,但可以相应地更新页面,如下所示:

app.service('pages').on('created', page => {
  // Assuming there is a reference of `users` on the page object
  page.users.forEach(user => {
    // Find all connections for this user
    const { connections } = app.channel(app.channels).filter(connection =>
      connection.user._id === user._id
    );

    app.channel('page-' + pageId).join(connections);
  });
});

答案 1 :(得分:0)

使用 app.channel('page-'+ pageId).join(connections) 不适用于我。但是循环遍历每个连接都可以:

connections.forEach(connection => {
  app.channel('page-' + userPage.pageId).join(connection)
})