【问题标题】:Pubnub is sending heart beat to multiple uuid?Pubnub 正在向多个 uuid 发送心跳?
【发布时间】:2021-08-22 06:18:46
【问题描述】:

当我使用更改 uuid 时


    React.useEffect(() => {
          console.log(window,'A')
          pubnub.destroy()
          pubnub.removeAllListeners()
          pubnub.removeAllListeners()
          pubnub.setUUID(myId);
          console.log(pubnub, 'Pubnub');
      }, [myId]);

它改变了uuid,但仍然调用以前的uuid?

【问题讨论】:

    标签: reactjs react-hooks pubnub


    【解决方案1】:

    猜测您的 pubnub 方法是异步的,因此您将在下次渲染时进行更改。

    【讨论】:

    • 您的回答似乎是“猜测”。作为评论/建议,这比作为答案更好;)
    【解决方案2】:

    销毁还是不销毁

    看起来如果你执行pubnub.destroy,那么pubnub.[anything] 将无法工作(虽然还没有实际测试过)。

    应该没有必要做destroy,但如果你真的想停止心跳,停止接收消息/事件并销毁pubnub对象,试试这个:

    // this should stop the heartbeats because it stops the subscribe call
    pubnub.unsubscribeAll();
    
    // this just stops removes the message/event listeners that 
    // receive published messages, presence events, status event, etc.
    pubnub.removeAllListeners();
    
    // this destroys the pubnub object for the GC to handle
    pubnub.destroy();
    

    打字稿

    如果使用 Typescript,方法 destroyremoveAllListeners 不会声明为可用类型,因此您只需执行以下操作:

    // this should stop the heartbeats because it stops the subscribe call
    pubnub.unsubscribeAll();
    
    // this destroys the pubnub object for the GC to handle
    pubnub.stop();
    

    更改 UUID

    如果要更改 UUID,则需要实例化一个新的 PubNub 对象并在配置中进行设置。

    以下代码可用于更改 UUID,但您可能只想先取消订阅所有频道,然后更改 UUID,然后再订阅新频道。

    React.useEffect(() => {
      console.log(window,'A')
      pubnub.unsubscribeAll();
      pubnub.setUUID(newUUID);
      console.log(pubnub, 'Pubnub');
    }, [newUUID]);
    

    请对此答案发表评论,让我知道您为什么需要更改 UUID,因为这对于大多数用例来说并不典型?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多