代码之家  ›  专栏  ›  技术社区  ›  musicformellons

FeatherJS通道的事件不会传递到客户端

  •  0
  • musicformellons  · 技术社区  · 6 年前

    我设置了一个非常基本的FeatherJS频道跟踪 their guide . 所以在服务器上我有:

    module.exports = app => {
      // If no real-time functionality has been configured just return
      if (typeof app.channel !== 'function') return
    
      app.on('connection', connection => {
        // On a new real-time connection, add it to the anonymous channel
        app.channel('anonymous').join(connection)
      })
    
      app.on('login', (authResult, {connection}) => {
        // connection can be undefined if there is no
        // real-time connection, e.g. when logging in via REST
        if (connection) {
          // Obtain the logged in user from the connection
          const {user} = connection
    
          // When the connection is no longer anonymous (as you the user is logged in), remove it
          app.channel('anonymous').leave(connection)
    
          // Add it to the authenticated user channel
          app.channel('authenticated').join(connection)
        }
      })
    
      app.publish((data, hook) => {
        return app.channel('authenticated')
      })
    
      app.service('points').publish('created', () => app.channel('authenticated'))
    }
    

    在我的客户中:

    api.on('authenticated', response => {
      console.log('Yes, here is the event from the channel: ', response)
    })
    

    这个设置应该提供所有FeatherJS服务中的所有事件。但是,我只在登录时在我的客户机上得到一个事件。当我随后通过Feathers API服务创建对象时,不会显示任何内容/不会显示任何事件。为什么不呢?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Daff    6 年前

    这个 authenticated event 是一个纯客户端事件,当客户端成功进行身份验证时将触发该事件。它不是从服务器发送的事件。

    频道仅适用于 service events 从服务器发送。对于您的示例,这意味着使用

    app.service('points').on('created', point => {})
    

    在客户身上。客户将只接收 created 事件。