订阅pubnub nodejs时回调不起作用

时间:2017-01-04 14:24:40

标签: javascript node.js node-modules pubnub

var PubNub = require("pubnub"); 
var pubnub = new PubNub({
    channel: 'Channel-xxxxxxxx',
    publishKey   : "pub-c-b91315c1-e6a1-xxxx-xxxx-xxxxxxxxxxxx",
    subscribeKey : "sub-c-b4735f6a-b6f6-xxxx-xxxx-xxxxxxxxxxxx" }); 

pubnub.subscribe({
    channel: 'Channel-xxxxxxxx',
    callback: function(message) {
        console.log("Message received: ", message);
    },
    error: function (err) {
        console.log(err);
    } });

使用pubnub文档上给出的示例代码仍然无法使其工作,不知道我哪里出错了

1 个答案:

答案 0 :(得分:0)

试试这个:

var PubNub = require("pubnub"); 
var pubnub = new PubNub({
    channel: 'Channel-xxxxxxxx',
    publishKey   : "pub-c-b91315c1-e6a1-xxxx-xxxx-xxxxxxxxxxxx",
    subscribeKey : "sub-c-b4735f6a-b6f6-xxxx-xxxx-xxxxxxxxxxxx" }); 

pubnub.subscribe({
    channel: 'Channel-xxxxxxxx',
    callback: function(message) {
        console.log("Message received: ", message);
    },
    error: function (err) {
        console.log(err);
    },
    connect: publish
 });