Example #1
0
        private void RunSubscribeRequest(long timetoken, bool reconnect)
        {
            //Exit if the channel is unsubscribed
            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog("in  RunSubscribeRequest", PNLoggingMethod.LevelInfo);
            #endif

            if (CheckAllChannelsAreUnsubscribed())
            {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog("All channels unsubscribed", PNLoggingMethod.LevelInfo);
                #endif

                return;
            }
            List <ChannelEntity> channelEntities = PubNubInstance.SubscriptionInstance.AllSubscribedChannelsAndChannelGroups;

            // Begin recursive subscribe
            try {
                long lastTimetoken = SaveLastTimetoken(timetoken);

                hbWorker.RunHeartbeat(false, hbWorker.HeartbeatInterval);
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("RunRequests: Heartbeat started"), PNLoggingMethod.LevelInfo);
                #endif
                RequestSentAt = DateTime.UtcNow.Second;
                if (PubNubInstance.PNConfig.PresenceInterval > 0)
                {
                    PHBWorker.RunPresenceHeartbeat(false, PubNubInstance.PNConfig.PresenceInterval);
                }

                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("MultiChannelSubscribeRequest: Building request for {0} with timetoken={1}", Helpers.GetAllNamesFromChannelEntities(channelEntities, true), lastTimetoken), PNLoggingMethod.LevelInfo);
                #endif
                // Build URL
                string channelsJsonState = PubNubInstance.SubscriptionInstance.CompiledUserState;

                string channels      = Helpers.GetNamesFromChannelEntities(channelEntities, false);
                string channelGroups = Helpers.GetNamesFromChannelEntities(channelEntities, true);

                //v2
                string filterExpr = (!string.IsNullOrEmpty(this.PubNubInstance.PNConfig.FilterExpression)) ? this.PubNubInstance.PNConfig.FilterExpression : string.Empty;
                Uri    requestUrl = BuildRequests.BuildSubscribeRequest(
                    channels,
                    channelGroups,
                    lastTimetoken.ToString(),
                    channelsJsonState,
                    region,
                    filterExpr,
                    this.PubNubInstance,
                    this.QueryParams
                    );

                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("RunSubscribeRequest: {0}", requestUrl.OriginalString), PNLoggingMethod.LevelError);
                #endif

                RequestState requestState = new RequestState();
                requestState.OperationType = PNOperationType.PNSubscribeOperation;
                requestState.URL           = requestUrl.OriginalString;
                requestState.Timeout       = PubNubInstance.PNConfig.SubscribeTimeout;
                requestState.Pause         = 0;
                requestState.Reconnect     = reconnect;
                //http://ps.pndsn.com/v2/presence/sub-key/sub-c-5c4fdcc6-c040-11e5-a316-0619f8945a4f/uuid/UUID_WhereNow?pnsdk=PubNub-Go%2F3.14.0&uuid=UUID_WhereNow
                webRequestId = webRequest.Run(requestState);
            } catch (Exception ex) {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("MultiChannelSubscribeRequest: method:_subscribe \n channel={0} \n timetoken={1} \n Exception Details={2}", Helpers.GetAllNamesFromChannelEntities(channelEntities, true), timetoken.ToString(), ex.ToString()), PNLoggingMethod.LevelError);
                #endif
                this.RunSubscribeRequest(timetoken, false);
            }
        }