protected override void RunWebRequest(QueueManager qm)
        {
            RequestState requestState = new RequestState();

            requestState.OperationType = PNOperationType.PNWhereNowOperation;

            string channels = "";

            if ((ChannelsToUse != null) && (ChannelsToUse.Count > 0))
            {
                channels = String.Join(",", ChannelsToUse.ToArray());
            }

            string channelGroups = "";

            if ((ChannelGroupsToUse != null) && (ChannelGroupsToUse.Count > 0))
            {
                channelGroups = String.Join(",", ChannelGroupsToUse.ToArray());
            }

            if (string.IsNullOrEmpty(uuid))
            {
                uuid = this.PubNubInstance.PNConfig.UUID;
            }
            Uri request = BuildRequests.BuildSetStateRequest(
                channels,
                channelGroups,
                Helpers.BuildJsonUserState(ChannelEntities),
                uuid,
                this.PubNubInstance,
                this.QueryParams
                );

            base.RunWebRequest(qm, request, requestState, this.PubNubInstance.PNConfig.NonSubscribeTimeout, 0, this);
        }
Beispiel #2
0
        public void Async(Action <PNPresenceHeartbeatResult, PNStatus> callback)
        {
            this.Callback = callback;
            RequestState requestState = new RequestState();

            requestState.OperationType = OperationType;
            List <ChannelEntity> channelEntities = new List <ChannelEntity>();

            string channels = "";

            if ((ChannelsToUse != null) && (ChannelsToUse.Count > 0))
            {
                ChannelsToUse.RemoveAll(t => t.Contains(Utility.PresenceChannelSuffix));
                string[] chArr = ChannelsToUse.Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray();
                channels = String.Join(",", chArr);
                channelEntities.AddRange(Helpers.CreateChannelEntity(chArr, false, false, null, PubNubInstance.PNLog));
            }

            string channelGroups = "";

            if ((ChannelGroupsToUse != null) && (ChannelGroupsToUse.Count > 0))
            {
                ChannelGroupsToUse.RemoveAll(t => t.Contains(Utility.PresenceChannelSuffix));
                string[] cgArr = ChannelGroupsToUse.Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray();
                channelGroups = String.Join(",", cgArr);
                channelEntities.AddRange(Helpers.CreateChannelEntity(cgArr, false, true, null, PubNubInstance.PNLog));
            }

            if (connected)
            {
                PubNubInstance.SubWorker.PHBWorker.RunIndependentOfSubscribe = true;
                PubNubInstance.SubWorker.PHBWorker.ChannelGroups             = channelGroups;
                PubNubInstance.SubWorker.PHBWorker.Channels = channels;
                if (UserState != null)
                {
                    PubNubInstance.SubWorker.PHBWorker.State = Helpers.BuildJsonUserState(channelEntities);
                }
                else
                {
                    PubNubInstance.SubWorker.PHBWorker.State = "";
                }
                PubNubInstance.SubWorker.PHBWorker.StopPresenceHeartbeat();
                PubNubInstance.SubWorker.PHBWorker.RunPresenceHeartbeat(false, PubNubInstance.PNConfig.PresenceInterval);
            }
            else
            {
                PubNubInstance.SubWorker.PHBWorker.RunIndependentOfSubscribe = false;
                PubNubInstance.SubWorker.PHBWorker.ChannelGroups             = channelGroups;
                PubNubInstance.SubWorker.PHBWorker.Channels = channels;
                PubNubInstance.SubWorker.PHBWorker.StopPresenceHeartbeat();
                PubNubInstance.SubWorker.PHBWorker.RunPresenceHeartbeat(false, PubNubInstance.PNConfig.PresenceInterval);
            }
        }
        public bool TryUpdateOrAddUserStateOfEntity(ref ChannelEntity channelEntity, Dictionary <string, object> userState, bool edit)
        {
            bool stateChanged = false;

            if (channelEntitiesDictionary.ContainsKey(channelEntity.ChannelID))
            {
                string newState = this.PubNubInstance.JsonLibrary.SerializeToJsonString(userState);
                if (channelEntitiesDictionary [channelEntity.ChannelID].UserState != null)
                {
                    string oldState = Helpers.BuildJsonUserState(channelEntitiesDictionary [channelEntity.ChannelID].UserState);
                    if (!oldState.Equals(newState))
                    {
                        channelEntitiesDictionary [channelEntity.ChannelID].UserState = EditUserState(userState,
                                                                                                      channelEntitiesDictionary [channelEntity.ChannelID].UserState, edit);

                        stateChanged = true;
                    }
                }
                else
                {
                    channelEntitiesDictionary [channelEntity.ChannelID].UserState = userState;
                    stateChanged = true;
                }
            }
            else
            {
                channelEntity.ChannelParams.UserState    = userState;
                channelEntity.ChannelParams.IsSubscribed = false;
                channelEntitiesDictionary.Add(channelEntity.ChannelID, channelEntity.ChannelParams);
                stateChanged = true;
            }
            if (stateChanged)
            {
                channelEntity.ChannelParams.UserState = channelEntitiesDictionary[channelEntity.ChannelID].UserState;
            }
            ResetChannelsAndChannelGroupsAndBuildState();

            return(stateChanged);
        }
        public void ResetChannelsAndChannelGroupsAndBuildState()
        {
            AllPresenceChannelsOrChannelGroups.Clear();
            AllNonPresenceChannelsOrChannelGroups.Clear();
            ChannelsAndChannelGroupsAwaitingConnectCallback.Clear();
            AllChannels.Clear();
            AllChannelGroups.Clear();
            AllSubscribedChannelsAndChannelGroups.Clear();
            HasChannelGroups                    = false;
            HasChannels                         = false;
            HasPresenceChannels                 = false;
            HasChannelsOrChannelGroups          = false;
            CurrentSubscribedChannelsCount      = 0;
            CurrentSubscribedChannelGroupsCount = 0;

            foreach (var ci in channelEntitiesDictionary)
            {
                if (ci.Value.IsSubscribed)
                {
                    if (ci.Key.IsChannelGroup)
                    {
                        CurrentSubscribedChannelGroupsCount++;
                        AllChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                    else
                    {
                        CurrentSubscribedChannelsCount++;
                        AllChannels.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                    AllSubscribedChannelsAndChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));

                    if (ci.Key.IsPresenceChannel)
                    {
                        AllPresenceChannelsOrChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                    else
                    {
                        AllNonPresenceChannelsOrChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));
                    }

                    if (ci.Value.IsAwaitingConnectCallback)
                    {
                        ChannelsAndChannelGroupsAwaitingConnectCallback.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                }
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("ResetChannelsAndChannelGroupsAndBuildState: channelEntities subscription key/val {0} {1}", ci.Key.ChannelOrChannelGroupName, ci.Value.IsSubscribed), PNLoggingMethod.LevelInfo);
                #endif
            }
            if (CurrentSubscribedChannelGroupsCount > 0)
            {
                HasChannelGroups = true;
            }
            if (CurrentSubscribedChannelsCount > 0)
            {
                HasChannels = true;
            }
            if (AllPresenceChannelsOrChannelGroups.Count > 0)
            {
                HasPresenceChannels = true;
            }
            if (HasChannels || HasChannelGroups)
            {
                HasChannelsOrChannelGroups = true;
            }
            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog(string.Format("PrevCompiledUserState: {0}", CompiledUserState), PNLoggingMethod.LevelInfo);
            #endif

            CompiledUserState = Helpers.BuildJsonUserState(AllSubscribedChannelsAndChannelGroups);

            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog(string.Format("CompiledUserState: {0}", CompiledUserState), PNLoggingMethod.LevelInfo);
            #endif
        }