【问题标题】:Asynchronous publish and subscribe messages is not working in pubnub异步发布和订阅消息在 pubnub 中不起作用
【发布时间】:2017-11-24 12:10:08
【问题描述】:

我正在使用以下代码进行pubnub 连接。

pubnub = new PubNub({
    subscribeKey: "demo",
    publishKey: "demo",
        keepAlive: true,
})
pubnub.addListener({
    message: function(message){
        console.log(message.channel+' '+message.publisher+' '+message.message.name+' '+time)
},
    status: function(s) {
        console.log(s.category+" "+s.operation+" "+s.affectedChannels +" "+s.subscribedChannels +" "+s.affectedChannelGroups+" "+time+" "+s.message)
    }
 });
 pubnub.history(
 {
    //child0, new
    channel: 'child3',
    reverse: true,
    count: 10
 },
 function (status, response) {
    // console.log(response);
    console.log(response.messages);
 }
 );
 pubnub.subscribe({
     channels: ['child3'],
     // resubscribe and reconnect
     restore : true, 
     //callback: function(m){console.log(m)} ,
 });

我有两个 nodejs 脚本,一个用于发布,另一个用于订阅 pubnub 中的消息。

当订阅者在线时,它能够接收来自发布者的消息,但是当订阅者离线时,由于错误的订阅脚本导致网络连接故障,它无法从发布者接收适当的消息.

请解决这个问题。

【问题讨论】:

  • 您的发布和订阅代码在哪里?顺便说一句,您的heartbeatInterval 值太低了。只需删除该行,presenceTimeout 设置就会自动将heartbeatInterval 配置为最佳值。
  • 这是否解决了您的 pub/sub 无法正常工作的问题?

标签: javascript node.js pubnub


【解决方案1】:

PubNub 在线订阅与离线订阅

当订阅者在线时,它能够接收来自发布者的消息,但是当订阅者离线时,由于错误的订阅脚本导致网络连接故障,它无法从发布者接收适当的消息.

这完全在意料之中。您说错误的订阅脚本,但它没有任何错误。除非您已连接,否则任何事情都不会奏效。一旦您的应用离线,您希望该应用如何接收任何消息?所以 PubNub 没有错误。它可以正常工作。

建立联系,您将收到消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2015-03-01
    相关资源
    最近更新 更多