检测到页面更改时取消订阅Action Cable



我正在Rails中构建一个聊天室类型的系统,我需要检测用户何时离开服务器上的频道(即关闭页面),以便更新活动用户列表。我在GitHub上搜索了文档和示例,似乎找不到答案。

我过去使用过SignalR,它会触发断开连接事件,但在Rails5中找不到等效的(如果它确实存在的话)。

我可以在channel类中看到带有取消订阅方法的示例,但无论我是导航到新页面还是完全关闭浏览器,都不会触发此操作。

编辑:不确定这是否有帮助,但我不使用涡轮链接。

这是Rails5测试版2的一个问题。我已经在最新版本beta 4中尝试过了,现在它像预期的那样被触发了。

以下是github问题供参考:https://github.com/rails/rails/pull/23715

试试这个

App.chatroom.disconnected()
=> yourChannel stopped streaming from chatroom #rails terminal
App.chatroom = App.cable.subscriptions.create({channel: 
'yourChannel'}, {
disconnected: function () {
App.cable.subscriptions.remove(this)
this.perform('unsubscribed') #in the channel have a method 'unsubscibed' to update users
},
)}

最新更新