通过 Nuxtjs 中的 Laravel Echo 获取实时 Pusherjs 连接状态
Get real time Pusherjs connection state via Laravel Echo in Nuxtjs
我想在 NuxtJs 页面中从 $echo
检查 Pusherjs
的连接状态。
当我使用:
mounted(){
this.connection = this.$echo.connector.pusher.connection.state;
}
它可以工作,但它只设置初始连接状态,即 connecting
并且它是静态的。
但是当我使用时:
computed:{
connection: this.$echo.connector.pusher.connection.state
}
它returns错误:
TypeError
Cannot read property '$echo' of undefined
那么如何实时通知我连接状态?
我找到了解决方案:
this.$echo.connector.pusher.connection.bind("state_change", (states) => {
// states = {previous: 'oldState', current: 'newState'}
});
参考:
https://pusher.com/docs/channels/using_channels/connection#binding-to-all-state-changes
我想在 NuxtJs 页面中从 $echo
检查 Pusherjs
的连接状态。
当我使用:
mounted(){
this.connection = this.$echo.connector.pusher.connection.state;
}
它可以工作,但它只设置初始连接状态,即 connecting
并且它是静态的。
但是当我使用时:
computed:{
connection: this.$echo.connector.pusher.connection.state
}
它returns错误:
TypeError
Cannot read property '$echo' of undefined
那么如何实时通知我连接状态?
我找到了解决方案:
this.$echo.connector.pusher.connection.bind("state_change", (states) => {
// states = {previous: 'oldState', current: 'newState'}
});
参考: https://pusher.com/docs/channels/using_channels/connection#binding-to-all-state-changes