如何在javascript中重新连接互联网后如何重新连接Pubnub?

问题描述 投票:2回答:1

我有一个Pubnub实例,我想知道当Internet断开并像给定的重试次数一样重新出现时如何处理重新连接吗?该文档肯定给出了适当的docs,但我无法将其放入代码中。

非常感谢您的帮助。我的代码:

this.pubnub = new PubNub({
  subscribeKey: this.serverDetails.authInfo.subscribeKey,
  authKey: this.serverDetails.authInfo.authKey,
  uuid,
  ssl: true
});

this.listeners = {
  message: msgEvent => {
    console.log(msgEvent);
  },
  status: statusEvent => {
    if (statusEvent.category === "PNNetworkDownCategory") {
      console.log(`network connection disappeared.`);
    }
    if (statusEvent.category === "PNNetworkUpCategory") {
      this.pubnub.reconnect();
    }
    if (statusEvent.category === "PNReconnectedCategory") {
      console.log("RECONNECTED");
    }
  }
};

this.pubnub.addListener(this.listeners);
javascript pubnub
1个回答
0
投票

在初始代码中设置restore:true

this.pubnub = new PubNub({
  subscribeKey: this.serverDetails.authInfo.subscribeKey,
  authKey: this.serverDetails.authInfo.authKey,
  uuid,
  ssl: true,
  restore: true // this allows reconnect to restore your channel subscription
});
© www.soinside.com 2019 - 2024. All rights reserved.