Pubnub.getMessageEventNameFor()不会触发

时间:2017-06-09 17:13:37

标签: javascript angularjs pubnub

我花了好几天时间弄明白这一点,但我不明白这个问题,从亲戚档案中的Pubnub代码开始:

文件1)其中,事件没有触发:

this.$rootScope.$on(this.Pubnub.getMessageEventNameFor("activity_" + this.activity.id + "_tables"), function (ngEvent, envelope) {
        console.log("envelope", envelope);
    });

文件2)(它是文件1的副本),其中事件触发器(只是更改了频道名称):

this.$rootScope.$on(this.Pubnub.getMessageEventNameFor("kitchen_" + this.activity.id + "_tables"), function (ngEvent, envelope) {
                console.log("envelope", envelope);
            });
触发事件的

文件3)

this.Pubnub.publish({
            channel: "kitchen_" + sessionStorage.getItem('activity_id') + "_tables",
            message: angular.toJson({ msg: "New Table", table: new_table})
        }, function (status, response) {
        });

this.Pubnub.publish({
            channel: "activity_" + sessionStorage.getItem('activity_id') + "_tables",
            message: angular.toJson({ msg: "New Table", table: new_table})
        }, function (status, response) {
        });
  • activity.id 所有(已测试)
  • 相同
  • 我在文件1 中制作了一个console.log(此。$ rootScope)并且该事件存在:
  

$$听众[" pubnub:默认:订阅:回调:activity_SUyNTii1He_tables"] [" 0"]

(如果我将鼠标移过,我可以看到该功能的主体,如果我点击"显示功能定义"它将我带到该功能,因此它具有该功能)

  • 当我从文件3 触发事件时......在文件1 中,只有控制台打印行:
  

pubnub.4.3.3.js:1670 XHR完成加载:GET" http://ps1.pubnub.com/v2/subscribe/MY_KEY_AND_UUID&pnsdk=PubNub-JS-Web%2F4.3.3"。

如果我点击打开新标签中的链接,它会显示正确的JSON结构,其中包含从文件3 传递的所有数据:

{
"t": {
    "t": "14970265478466724",
    "r": 12
},
"m": [
    {
        "a": "0",
        "f": 0,
        "i": "...other-key...",
        "p": {
            "t": "14970265478474153",
            "r": 12
        },
        "k": "...subscribe-key...",
        "c": "activity_SUyNTii1He_tables",
        "d": "{
            ...all my data...
        }",
        "b": "activity_SUyNTii1He_tables"
    }
]

}

我不太了解Pubnub,所以我不知道我是否遗漏了它...

更新 我已经解决了这个问题。在我的"页面"文件2中有两个标签可以完成我的html页面,每个标签都是来自"组件"每个文件都有一个Pubnub初始化。在同一"页面上进行两次初始化"创建此问题,因此删除一个(我已删除文件2 中的Pubnub初始化)将解决此问题。 也许它对未来这个问题很有帮助。 Angular中的逻辑错误是我没有想到这两个组件文件就像页面文件中的一个

2 个答案:

答案 0 :(得分:1)

您是否注册了活动? 例如:

Pubnub.subscribe({
    channel: $scope.channel, 
    triggerEvents: ['event1', 'event2', 'event3', '...']
});

答案 1 :(得分:0)

Op的解决方案对我不起作用,但我通过更改订阅时使用所选频道的数组的channel属性来解决问题。

例如:

Pubnub.subscribe({
    channels: [ $scope.channel ],
    triggerEvents: true
});

https://github.com/pubnub/pubnub-angular#events

希望它有所帮助。

相关问题