Pubnub:无法通过状态事件获取状态对象

时间:2016-07-05 12:08:27

标签: javascript angularjs pubnub

我正在尝试在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"
    }
}

我已经被这件事困住了一段时间了。 :(

请告诉我这里做错了什么。为什么没有在状态事件中设置状态。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

最后。由于pubnub的支持,我已经能够检测到这个问题。 这是由于错误的事件发生时间。 在我的代码中,here_now事件在设置其状态的用户的presence事件之前被触发。

现在我在用户自己的join事件中设置状态,并在state-change事件中调用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});
                            }
                        }
                    }
                });
            }
        });
    }