我有一个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);
在初始代码中设置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
});