【问题标题】:How to get connection status / other events in laravel Echo如何在 laravel Echo 中获取连接状态/其他事件
【发布时间】:2019-06-24 19:04:44
【问题描述】:

我开发了一个仪表板,它通过自定义事件获取实时通知。我想要实现的是能够在频道之间切换。我使用 Vue(x) 作为前端。

created() {
    this.setTeam(this.$router.currentRoute.name);
    this.clearFastlanes();
    this.getSprintInfo('getSprintInfo');
    window.Echo.channel(this.getTeam)
      .listen('.jira.issue', this.jiraIssue)
      .listen('.jira.fastlane', this.jiraFastlane);
  },

这是我订阅自定义事件的主要组件。唯一的问题是,这个组件也被另一个团队使用。所以我想将我的频道更改为当前团队(离开当前频道并加入新团队的频道)。我真的无法在互联网上找到如何做到这一点。

这就是我的连接方式:

window.Echo = new Echo({
  broadcaster: 'pusher',
  key: '29564c255fe1dae3654b',
  cluster: 'eu',
  encrypted: true,
});

所以基本上我想问 laravel echo 我当前是否已连接,如果是,请离开频道并连接到新团队。

有人知道如何实现这一目标吗?

【问题讨论】:

    标签: laravel vue.js pusher laravel-echo


    【解决方案1】:

    对于连接:

        window.Echo.connector.pusher.connection.bind('connected', () => {
          console.log('connected');
        });
    

    断开连接:

        window.Echo.connector.pusher.connection.bind('disconnected', () => {
           console.log('disconnected);
        });
    

    【讨论】:

    【解决方案2】:

    Socket IO 解决方案

    以下是监听“连接”、“断开”和“重新连接”事件的方法。

    连接:

    window.Echo.connector.socket.on('connect', () => {
      //your code
    });
    

    断开连接:

    window.Echo.connector.socket.on('disconnect', () => {
      //your code
    });
    

    重新连接:

    window.Echo.connector.socket.on('reconnecting', (attemptNumber) => {
      //your code
      console.log(`%cSocket reconnecting attempt ${attemptNumber}`, 'color:orange; font-weight:700;');
    });
    

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 2020-04-26
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多