2017-09-17 84 views
0

刚刚开始使用PubNub,似乎我甚至无法理解最简单的情况。我创建了以下测试页:PubNub不断收到一条消息

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.15.1.js"></script> 
<script> 
const pubnub = new PubNub({ 
    publishKey : '<guid>', 
    subscribeKey : '<one more guid>' 
}); 
pubnub.subscribe({channels: ['3']}); 
pubnub.addListener({ 
    message: v => { 
    console.log("on message", v); 
    }, 
}); 
function onClick() { 
    pubnub.publish({channel: '3', message: 'foo'}); 
} 
</script> 
</head> 
<body> 
<button onclick="onClick()">start</button> 
</body> 
</html> 

与最新的Chrome,然后点击打开它的“开始”按钮,就会被无休止地收到了个遍测试消息。我的印象是,在单个客户端收到来自总线的消息后,该客户端不会再收到它。为什么这样的行为据我所知,我可以阅读all文档,最有可能的答案是内部深处,但教程+快速启动没有提供任何线索,其余文档相当庞大。

enter image description here

回答

1

你的示例代码完全适用于我。发布的消息在频道“3”上接收一次。验证此方法的一种方法是同时打开PubNub控制台(https://www.pubnub.com/docs/console)。确保您将发布和订阅键与频道“3”一起输入到控制台中。点击PubNub控制台中的“订阅”按钮后,每次单击测试页上的“开始”按钮时,您应该会在底部的“消息”部分中看到您的测试消息“foo”出现一次。

+0

我得到了相同的结果托德 - 单个消息。您正在使用MS Edge(从未尝试过),那么您可以尝试使用Chrome和Firebox,并告诉我们您是否得到相同或不同的结果? –

+0

我正在使用最新的Chrome。它显示在我的屏幕截图上。为什么是Edge? @CraigConover – grigoryvp

+0

哦,nm,我在您的路径中看到* edge *并出于某种原因假定Edge浏览器。所以我们不确定这里发生了什么。必须有一些其他外部演员导致这种情况发生,因为它不是预期的,我们无法复制它。我看到你提交了PN支持。即使您确定它与100%以上的相同,您是否可以附加该票中的完整源文件?非常感谢! –

1

我可以看到你正在使用最新的SDK-JS V4(完美起点)

  • 你的代码的工作!

我想指出你有点不同的方式来初始化PubNub和几个功能。

(这是在他们的文档可用)

请看附链接查看我的PubNub demo

<script type="text/javascript"> 
     console.log('init PubNub.');  
     pubnub = new PubNub({ 
     publishKey: 'demo', 
     subscribeKey: 'demo', 
     uuid: 'myDemo' 
     }) 

     console.log("addListener.."); 
     pubnub.addListener({ 
     status: function(statusEvent) { 
      if (statusEvent.category === "PNConnectedCategory") { 
      console.log("PNConnectedCategory.."); 
      publishSampleMessage(); 
      } 
     }, 
     message: function(message) { 
      console.log("New Message!!", message.message);   
     }, 
     presence: function(presenceEvent) { 
      //handle presence 
     } 
     }) 

     console.log("Subscribing.."); 
     pubnub.subscribe({ 
     channels: ['myDemo'] 
     }); 

     function publishSampleMessage() { 
     console.log("Since we're publishing on subscribe connectEvent, we're sure we'll receive the following publish."); 
     var publishConfig = { 
      channel: "myDemo", 
      message: "I'm here, I'm alive!!" 
     } 
     pubnub.publish(publishConfig, function(status, response) { 
      console.log(status, response); 
     }) 
     } 

    function onClick() { 
     publishSampleMessage(); 
    } 

    </script> 
相关问题