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



这是我的订阅代码,我想获得channelid,所以我在代码中使用了This.channel,但我没有定义。有什么方法可以让我获得通道ID 吗

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));
    }
})

结果:通道=未定义

看看精细的手册,这应该是可行的:

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()的对象的上下文中调用。

最新更新