如何在pubnub订阅/或发布功能中获取channelid?

时间:2015-06-25 07:59:35

标签: node.js pubnub

这个我的订阅代码我想获得channelid,所以我在代码中使用了 this.channel ,但我得到了未定义。有什么方法可以获得channelid

pubnub.subscribe({
    channel: changing dynamically,
    presence: function (m) {
        console.log(m)
    },
    message: function (m) {
        console.log(m);
        console.log("Channel ==" + this.channel)
    },
    error: function (error) {
        // Handle error here
        console.log(JSON.stringify(error));
    }
})

结果: 通道==未定义

1 个答案:

答案 0 :(得分:2)

查看the fine manual,这应该有效:

pubnub.subscribe({
    channel: changing dynamically,
    presence: function (m) {
        console.log(m)
    },
    message: function (m, env, channel) {
        console.log(m);
        console.log("Channel ==" + channel)
    },
    error: function (error) {
        // Handle error here
        console.log(JSON.stringify(error));
    }
})

换句话说,频道作为参数传递给message回调。

this.channel未定义的最可能原因是message回调未在传递给pubnub.subscribe()的对象的上下文中调用。

相关问题