Pubnub:无法获得带有存在事件的状态对象
Pubnub: cannot get state object with presence event
我试图在Pubnub文档中记录的存在事件中获取数据(状态)对象。
这是我的代码:-
// Subscribe to messages channel
Pubnub.subscribe({
channel: $scope.channel,
triggerEvents: ['callback','presence','message'],
state: {
username : $scope.username,
membershipType : $scope.membershipType,
memberDealsIn : $scope.memberDealsIn
}
});
//Track online users
Pubnub.here_now({
channel : $scope.channel,
state: true,
callback : function(m){
$scope.$apply(function() {
$scope.onlineUsers.push(m['uuids'])
});
}
});
//User's State
Pubnub.state({
channel : $scope.channel,
state : {
username : $scope.username,
membershipType : $scope.membershipType,
memberDealsIn : $scope.memberDealsIn
},
callback : function(m){
//console.log(m)
},
error : function(m){
//console.log(m)
}
});
我从Pubnub的getPresenceEventNameFor方法中调用它:-
$scope.$on(Pubnub.getPresenceEventNameFor($scope.channel), function (ngEvent, pnEvent) {
console.log("pnEvent: ", pnEvent);
}
下面是我的输出:-
pnEvent: Object {action: "join", uuid: "1310974", timestamp: 1467719168, occupancy: 3}
正如你所看到的,其他一切都很好,但我无法从中获取数据。然而文档说它也应该有数据,比如:-
{
"action" : "join",
"timestamp" : 1345546797,
"uuid" : "175c2c67-b2a9-470d-8f4b-1db94f90e39e",
"occupancy" : 2,
"data" : {
"age" : 67,
"full" : "RobertPlant",
"country" : "UK",
"appstate" : "foreground",
"latlong" : "51.5072°N,0.1275°W"
}
}
我已经被这件事困扰了一段时间了。(请告诉我哪里做错了。为什么不在状态事件中设置状态
谢谢你的帮助。
终于。由于公众的支持,我已经能够发现这个问题。这是由于事件发生的时机不对。在我的代码中,here_now事件在用户的状态被设置之前被触发。
现在我在用户自己的连接事件中设置状态,并在状态更改事件中调用here_now事件。
//Presence Callback
$scope.$on(Pubnub.getPresenceEventNameFor($scope.channel), function (ngEvent, pnEvent, envelope, channel) {
//Detect Join events for users
if (pnEvent['action'] === 'join') {
//Detect User's own join event and set state for the user
if (pnEvent['uuid'] === $scope.uuid) {
//User's State
Pubnub.state({
channel : $scope.channel,
state : {
username : $scope.username,
membershipType : $scope.membershipType,
memberDealsIn : $scope.memberDealsIn
},
callback : function(m){
//console.log(m)
},
error : function(m){
//console.log(m)
}
});
}
}
//Detect state change event
if (pnEvent['action'] === 'state-change') {
//Call here_now on state change of a user
//Track online users
Pubnub.here_now({
channel : $scope.channel,
state: true,
callback : function(m){
$scope.$apply(function() {
for (var userIdx in m['uuids']) {
var user = m['uuids'][userIdx];
//Push new user to online User's list if not there
if (!_.find($scope.onlineUsers, {uuid: user.uuid}) && (user.uuid != $scope.uuid)){
//Push only if state is not undefined
if (user.state.membershipType != null) {
$scope.onlineUsers.push({uuid: user.uuid, membership: user.state.membershipType, dealsin: user.state.memberDealsIn, username: user.state.username});
}
}
}
});
}
});
}
相关文章:
- 正在将事件处理程序添加到不存在的类
- 存在每个时间元素的javascript事件
- 将事件侦听器添加到文档,而不是签入元素存在,然后添加事件侦听器
- 存在其他参数时访问Javascript事件
- 事件处理程序连接在哪里,以及它为什么存在
- 事件侦听器更新就绪在加载时未显示确认,而更新实际存在
- 是否存在添加两个相同事件处理程序的默认行为
- 存在事件的单元格的jQuery DatePicker背景色(加载时)
- 通过它查询谷歌地图上已经存在的标记's的lat和long,并以编程方式在其上触发一个单击事件
- CollectionFS中是否存在用于文件下载的事件侦听器/回调
- 当您尝试添加不存在的事件时,addEventListener 的行为是什么
- 当页面上存在特定文本字符串时发送 Google Analytics(分析)事件
- 是否存在渲染 CollectionView 中的所有项后触发的木偶.js事件
- 使用 on() 将事件处理程序添加到尚不存在的元素
- 如果事件附加到不存在的元素,是否可以触发该事件
- 查找绑定在不存在元素上的事件
- 使用核心页面,是否存在页面加载事件
- 订阅者(对于自定义事件)不存在.骨干.js
- 从可放置区域删除可拖动项目时是否存在事件
- 链接单击事件存在