Detect Action Cable connection status

自古美人都是妖i 提交于 2020-01-24 14:03:02

问题


I have a Vue app that connects to a Rails back-end and need to find out whether the connection is established or not.

In my app I do:

var app = new Vue({
    name: "Site",
    store,
    router,
    i18n,
    components: {},
    mixins: [],
    data: {
     cable: App.cable
    }
});

However cable.connection.disconnected is always true even when actually connected.

How can I reliably figure out (when needed) what the connection status is?


回答1:


Got it sorted out. In my components, when I need to find out cable status, I do

this.$root.cable.connection.isOpen()

If false I revert to HTTP.

Hope this helps someone later on.




回答2:


You can try broadcast some message on server and watch Vue app can receive that message.



来源:https://stackoverflow.com/questions/59595604/detect-action-cable-connection-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!