internal bool UpdateOrAddUserStateOfEntity(string channel, bool isChannelGroup, Dictionary <string, object> userState, bool edit, bool isForOtherUUID, ref List <ChannelEntity> channelEntities)
        {
            ChannelEntity ce           = Helpers.CreateChannelEntity(channel, false, isChannelGroup, userState, this.PubNubInstance.PNLog);
            bool          stateChanged = false;

            if (isForOtherUUID)
            {
                ce.ChannelParams.UserState = userState;
                channelEntities.Add(ce);
                stateChanged = true;
            }
            else
            {
                stateChanged = this.PubNubInstance.SubscriptionInstance.TryUpdateOrAddUserStateOfEntity(ref ce, userState, edit);
                if (!stateChanged)
                {
                    PNStatus pnStatus = base.CreateErrorResponseFromMessage("No change in User State", null, PNStatusCategory.PNUnknownCategory);
                    Callback(null, pnStatus);
                }
                else
                {
                    channelEntities.Add(ce);
                }
            }
            return(stateChanged);
        }
Example #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);
            }
        }
        internal bool CreateChannelEntityAndAddToSubscribe(PNOperationType type, List <string> rawChannels, bool isChannelGroup, bool unsubscribeCheck, ref List <ChannelEntity> channelEntities, PubNubUnity pn)
        {
            bool bReturn = false;

            for (int index = 0; index < rawChannels.Count; index++)
            {
                string channelName = rawChannels[index].Trim();

                if (channelName.Length > 0)
                {
                    if ((type == PNOperationType.PNPresenceOperation) || (type == PNOperationType.PNPresenceUnsubscribeOperation))
                    {
                        channelName = string.Format("{0}{1}", channelName, Utility.PresenceChannelSuffix);
                    }

                    #if (ENABLE_PUBNUB_LOGGING)
                    LogChannelEntitiesDictionary();
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("CreateChannelEntityAndAddToSubscribe: channel={0}", channelName), PNLoggingMethod.LevelInfo);
                    #endif

                    //create channelEntity
                    ChannelEntity ce = Helpers.CreateChannelEntity(channelName, true, isChannelGroup, null, this.PubNubInstance.PNLog);

                    bool channelIsSubscribed = false;
                    if (ChannelEntitiesDictionary.ContainsKey(ce.ChannelID))
                    {
                        channelIsSubscribed = ChannelEntitiesDictionary [ce.ChannelID].IsSubscribed;
                    }

                    if (unsubscribeCheck)
                    {
                        if (channelIsSubscribed)
                        {
                            channelEntities.Add(ce);
                            bReturn = true;
                        }
                        #if (ENABLE_PUBNUB_LOGGING)
                        else
                        {
                            string message = string.Format("{0}Channel Not Subscribed", (ce.ChannelID.IsPresenceChannel) ? "Presence " : "");
                            this.PubNubInstance.PNLog.WriteToLog(string.Format("CreateChannelEntityAndAddToSubscribe: channel={0} response={1}", channelName, message), PNLoggingMethod.LevelInfo);
                        }
                        #endif
                    }
                    else
                    {
                        if (!channelIsSubscribed)
                        {
                            channelEntities.Add(ce);
                            bReturn = true;
                        }
                        #if (ENABLE_PUBNUB_LOGGING)
                        else
                        {
                            string message = string.Format("{0}Already subscribed", (ce.ChannelID.IsPresenceChannel) ? "Presence " : "");
                            this.PubNubInstance.PNLog.WriteToLog(string.Format("CreateChannelEntityAndAddToSubscribe: channel={0} response={1}", channelName, message), PNLoggingMethod.LevelInfo);
                        }
                        #endif
                    }
                }
                #if (ENABLE_PUBNUB_LOGGING)
                else
                {
                    string message = "Invalid Channel Name";
                    if (isChannelGroup)
                    {
                        message = "Invalid Channel Group Name";
                    }

                    this.PubNubInstance.PNLog.WriteToLog(string.Format("CreateChannelEntityAndAddToSubscribe: channel={0} response={1}", channelName, message), PNLoggingMethod.LevelInfo);
                }
                #endif
            }
            return(bReturn);
        }