【问题标题】:Centrifuge + React离心机+反应
【发布时间】:2022-09-25 14:48:54
【问题描述】:

在我们的应用程序中,我们使用离心机作为网络套接字连接 + 反应。 当需要刷新令牌时,我使用配置功能 - onRefresh,然后我收到新令牌并设置它。

refreshHeaders: { Authorization: `Bearer ${localStorage.getItem(\"act\")}` },
  onRefresh: async function (ctx, cb) {
    try {
      await axios
        .post(`${BASE_URL}/centrifugo/refresh`, null, {
          headers: {
            Authorization: `Bearer ${localStorage.getItem(\"act\")}`,
          },
        })
        .then((data) => {
          centrifuge.setToken(data.token);
        });
    } catch {
      centrifuge.disconnect();
      console.log(\"resfresh failed 1\");
    }
  },

但在 ws 连接中出现错误 - 代码:109,令牌已过期 我的方式有什么问题?

    标签: reactjs centrifuge


    【解决方案1】:

    对于提出此问题的 centrifuge-js v2.x - 解决方案是在从后端获取令牌时调用 cb(由 SDK 在 onRefresh 参数中传递)而不是 centrifuge.setToken。

    centrifuge-js v3.x 用于令牌刷新的 API 略有不同,described here

    【讨论】:

      猜你喜欢
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多