我花了几天时间来解决这个问题,但我不明白这个问题,从亲属文件中的 Pubnub 代码开始:
文件 1) 事件不会触发:
this.$rootScope.$on(this.Pubnub.getMessageEventNameFor("activity_" + this.activity.id + "_tables"), function (ngEvent, envelope) {
console.log("envelope", envelope);
});
文件 2)(它是文件 1 的副本),其中事件触发(只是更改了 channel 名称):
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(this.$rootScope) 并且事件存在:
.$$listeners["pubnub:default:subscribe:callback:activity_SUyNTii1He_tables"]["0"]
(如果我将鼠标悬停在上面,我可以看到函数的主体,如果我单击“显示函数定义”,它会将我带到函数,因此它具有函数)
- 当我从文件 3 触发事件时...在文件 1 中只有一条控制台打印行:
pubnub.4.3.3.js:1670 XHR finished loading: 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 初始化。在同一个“页面”上进行两个初始化会产生此问题,因此删除一个(我已删除 File 2 中的 Pubnub Initialization)将解决此问题。 也许这对将来这个问题有用。 Angular 中的逻辑错误是我没有将这 2 个组件文件视为页面文件中的一个
最佳答案
您是否注册了该事件? 例如:
Pubnub.subscribe({
channel: $scope.channel,
triggerEvents: ['event1', 'event2', 'event3', '...']
});
关于javascript - Pubnub.getMessageEventNameFor() 不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44463405/