Pubnub:无法获得带有存在事件的状态对象

Pubnub: cannot get state object with presence event

本文关键字:事件 存在 状态 对象 Pubnub      更新时间:2023-09-26

我试图在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});
                            }
                        }
                    }
                });
            }
        });
    }