销毁还是不销毁
看起来如果你执行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,方法 destroy 和 removeAllListeners 不会声明为可用类型,因此您只需执行以下操作:
// 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,因为这对于大多数用例来说并不典型?