如何在 laravel Echo 中获取连接状态/其他事件



我开发了一个仪表板,可以通过自定义事件获取实时通知。我想要实现的是能够在频道之间切换。我正在使用 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 我目前是否连接,如果是,请离开频道并连接到新团队。

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

对于连接:

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

对于断开连接:

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

套接字 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;');
});

最新更新