【问题标题】:How to get channelid in pubnub subscribe/or publish function?如何在 pubnub 订阅/或发布功能中获取 channelid?
【发布时间】:2015-06-25 07:59:35
【问题描述】:

这是我的订阅代码,我想获取频道 ID,所以我在代码中使用了 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));
    }
})

结果: 频道==未定义

【问题讨论】:

    标签: node.js pubnub


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      相关资源
      最近更新 更多