Pubnub:无法通过存在事件获取状态对象
Pubnub: cannot get state object with presence event
我正在尝试获取存在事件中的数据(状态)对象,如 Pubnub Documentation 中所述。
这是我的代码:-
// 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}
如您所见,其他一切都很好,但我无法在其中获取数据。
而 Documentation 表示它也应该有数据,比如 :-
{
"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"
}
}
我已经被这个问题困扰了一段时间。 :(
请告诉我我做错了什么。为什么不在出席事件中设置状态。
感谢您的帮助。
终于。感谢 pubnub 的支持,我已经能够检测到这个问题。
这是由于事件发生的时间错误。
在我的代码中,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});
}
}
}
});
}
});
}
我正在尝试获取存在事件中的数据(状态)对象,如 Pubnub Documentation 中所述。
这是我的代码:-
// 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}
如您所见,其他一切都很好,但我无法在其中获取数据。 而 Documentation 表示它也应该有数据,比如 :-
{
"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"
}
}
我已经被这个问题困扰了一段时间。 :(
请告诉我我做错了什么。为什么不在出席事件中设置状态。
感谢您的帮助。
终于。感谢 pubnub 的支持,我已经能够检测到这个问题。 这是由于事件发生的时间错误。 在我的代码中,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});
}
}
}
});
}
});
}