2015-06-25 42 views
1

這是我的訂閱代碼,我想要獲取channelid,所以我在代碼中使用了this.channel,但我沒有定義。有什麼辦法,我能得到的channelID如何在pubnub訂閱/發佈函數中獲取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)); 
    } 
}) 

結果: 通道==未定義

回答

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未定義的最可能的原因是在傳遞到pubnub.subscribe()的對象的上下文中不調用message回調。