订阅多个频道时,我如何知道消息来自哪个频道?

When subscribed to multiple channels, how do I know which channel a message came in on?

根据文档,您可以像这样订阅多个频道:

pubnub.subscribe({
    channel : ['my_channel_1','my_channel_2','my_channel_3'],
    message : function(message) {
        console.log(message) 
    }
});

我的应用订阅了 TESTLIVE。我的问题是,在 message 事件中是否有任何方法可以确定它来自哪个通道而不将其包含在有效负载中? (我确认 m 是我发送的完整对象,仅此而已)。

PubNub 监听消息回调参数

传递给 message 侦听器的参数具有以下结构:

I edited/updated the channel and subscription key names below. The old key names were actualChannel and subscribedChannel but these are deprecated as of PubNub JavaScript SDK v4.0.9 because they were confusing.

{
    "subscription": undefined, // as of SDK v4.0.10
    "channel": "ch1", // as of SDK v4.0.10
    "timetoken": "14721821326909151", // publish timetoken
    "message": {"data":"hello"} // the message
}

// example code to get the data above that is passed into 'message'
pubnub.addListener({
    message: function(m) {
        // handle message
        var channel = m.channel;
        var pubTT = m.timetoken;
        var msg = m.message;
    },
    presence: function(p) {
        // handle presence
    },
    status: function(s) {
        // handle status
    }
});

注意:关于subscription属性

  • 如果客户端订阅了频道组而不是直接订阅了频道,那么subscription属性将保存频道所在的频道组的值。
        subscription: channelGroupFoo
        channel: channelBar
  • 如果客户端是通过 wildcard 而不是直接订阅到 channel,那么 subscription 属性将是通配符模式匹配 通道 是其中的一部分。
        subscription: foo.*
        channel: foo.bar
  • 但是 channel 属性将始终是消息发布 to/received 的 channel

PubNub JSV3中,您可以使用message获取消息的频道名称 ] 打回来。

这里是PubNub JSV3的示例代码:

pubnub.subscribe({
    channel : ['my_channel_1','my_channel_2','my_channel_3'],
    message : function( message, env, channel ){
        console.log(message);
        console.log(channel)
    },
    connect : function(){
        console.log("Connected")
    },
    disconnect : function(){
        console.log("Disconnected")
    },
    reconnect : function(){
        console.log("Reconnected")
    },
    error : function(){
        console.log("Network Error")
    }, 
})

PubNub JSV4 中,您可以使用 addListener 获取消息的 频道名称 ] 功能。此功能还提供许多其他选项,例如 PubNub JSV3 中的连接状态(例如 'connected'、'disconnected' 等),所有这些都捆绑在订阅中。

这里是PubNub JSV4的示例代码:

pubnub.addListener({
    status: function(statusEvent) {
        if (statusEvent.category === "PNConnectedCategory") {
            var newState = {
                new: 'connected'
            };
            PubNub.setState(
                { 
                    state: newState 
                }, 
                function (status) {
                    console.log('connected', status)
                }
            );
        } else if (statusEvent.category === "PNUnknownCategory") {
            var newState = {
                new: 'error'
            };
            PubNub.setState(
                { 
                    state: newState 
                }, 
                function (status) {
                    console.log(statusEvent.errorData.message)
                }
            );
        } else if (statusEvent.category === "PNNetworkIssuesCategory") {
            var newState = {
                new: 'disconnected'
            };
            PubNub.setState(
                { 
                    state: newState 
                }, 
                function (status) {
                    console.log('disconnected', status)
                }
            );
        }
        else if (statusEvent.category === "PNReconnectedCategory") {
            var newState = {
                new: 'reconnect'
            };
            PubNub.setState(
                { 
                    state: newState 
                }, 
                function (status) {
                    console.log('reconnected', status)
                }
            );
        }
    },
    message: function(message) {
        console.log(message)
    },
    presence: function(presenceEvent) {
        console.log(presenceEvent)
    }
})

pubnub.subscribe({ 
    channels : ['my_channel_1','my_channel_2','my_channel_3']
});