如何在Pubnub中重新连接? [英] How to reconnect in Pubnub?

查看:89
本文介绍了如何在Pubnub中重新连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PubNub 在我的 Js 代码中传输通知.一旦互联网断开连接并恢复,我无法弄清楚如何在 PubNub 中重新连接.

I am using PubNub for notifications transfer across my Js code. I am unable to figure out how to reconnect in PubNub once internet disconnects and comes back up.

在我的初始化和执行中恢复:true

having restore: true in my init and doing

this.listeners = {
  message: msgEvent => {
    console.log(msgEvent);
  },
  status: statusEvent => {
    if (statusEvent.category === "PNNetworkUpCategory") {
      this.pubnub.reconnect();
}}};

对我不起作用.

完整代码:

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

this.listeners = {
  message: msgEvent => {
    console.log(msgEvent);
  },
  status: statusEvent => {
    if (statusEvent.category === "PNNetworkUpCategory") {
      this.pubnub.reconnect();
    }
  }
};

this.pubnub.addListener(this.listeners); 

SDK:4.27.2

期望:尝试重新连接 PubNub 最多 N 次尝试),订阅现有订阅频道.

expectation: try to Reconnect PubNub max N tries) ,subscribe to existing subscribed channels.

推荐答案

看着它,你可能会得到一些你没有检查的其他状态响应......此外,我认为你需要 autoNetworkDetection 标志使用状态 PNNetworkDownCategoryPNNetworkUpCategory 宣布网络何时关闭或开启.即

Looking at it you are possibly getting some other status response that you are not checking for...Also I think you would require the autoNetworkDetection flag to announce when the network is down or up using the states PNNetworkDownCategory and PNNetworkUpCategory. i.e.

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

this.listeners = {
  message: msgEvent => {
    console.log(msgEvent);
  },
  status: statusEvent => {
    if (statusEvent.category === "PNNetworkUpCategory") {
      this.pubnub.reconnect();
    } else {
      // check for other status events - PNTimeoutCategory, PNNetworkIssuesCategory, etc
      console.log(statusEvent.category);
    } 
  }
};

如果失败并且您仍然遇到重新连接问题,您应该将标志 listenToBrowserNetworkEvent 设置为 false,因为这允许 SDK 重新连接逻辑接管.即

If that fails and you still get reconnection issues you should set the flag listenToBrowserNetworkEvent to false as this allows the SDK reconnection logic to take over. i.e.

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

参见:https://www.pubnub.com/docs/web-javascript/pubnub-network-lifecycle#pnnetworkupcategory

这篇关于如何在Pubnub中重新连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆