我在their guide之后建立了一个非常基本的Featherjs频道。所以在服务器上我有:
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服务中的所有事件。但是,当我登录时,我只会在客户端上收到一个事件。当我随后通过我的Feather api服务创建对象时,什么也没有显示/没有事件通过。为什么不呢?
答案 0 :(得分:1)
authenticated event是一个纯粹的客户端事件,当客户端成功进行身份验证时将触发该事件。这不是从服务器发送的事件。
通道仅适用于从服务器发送的service events。对于您的示例,这意味着使用类似
的方法app.service('points').on('created', point => {})
在客户端上。客户端仅在通过身份验证后才会收到created
事件。