如何在推送器 js 中监听关闭事件

How to listen close event in pusher js

我们正在尝试根据推送器中创建的通道输出触发事件

import Pusher from "pusher-js";
import { logoutUser } from "../redux/actions/authActions";
import store from "../redux/store/store";

const authSessionPusher = (sessionId) => {
  const pusher = new Pusher("xxxxxxxxxxxxxxxx", {
    cluster: "ap2",
    encrypted: true
  });
  const channel = pusher.subscribe(sessionId);
  console.log(channel);
  channel.bind("authSession", (data) => {
    if (data.message === "session logout") {
      store.dispatch(logoutUser());
    }
  });
};

export { authSessionPusher };

我们能够做到这一点,但现在我们还想在推送通道连接超时时调用一个事件

我们应该添加什么类型的事件侦听器以在通道连接超时时获得回调以附加该事件的函数?

可以绑定到连接事件。它们记录在此处:https://pusher.com/docs/channels/using_channels/connection/#connection-states

您可以绑定到特定的连接事件 (list of available states)。

pusher.connection.bind("connected", function () {
  console.log("Connected!");
});

或者通过绑定到 state_change.

来绑定到所有连接状态更改
pusher.connection.bind("state_change", function (state) {
  console.log("Connection state", state);
});