Example #1
0
        /// <summary>
        /// Host app must call this when receiving http request on POST /api/retro/long
        /// </summary>
        public async Task <LongResponse> HandleHttpLong(LongRequest req)
        {
            var user = ClientPlex.GetUser(req.SessionKey);

            if (user == null)
            {
                return new LongResponse {
                           ErrorCode = Constants.ERRCODE_BADUSER
                }
            }
            ;

            //if there is already something to push, then return now, without any awaiting
            var pushGroup = ClientPlex.GetAndClearItemsToPush(req.SessionKey);

            if (pushGroup != null)
            {
                return(PushGroupToLongResponse(user, pushGroup));
            }

            //wait up to 30s for something to be ready to send to the client
            var pollTask = ClientPlex.BeginLongPoll(req.SessionKey);

            if (pollTask == null)
            {
                return new LongResponse {
                           ErrorCode = Constants.ERRCODE_BADUSER
                }
            }
            ;
            var  timeoutTask = Task.Delay(IntegrationTestMode ? 1 : 30000);
            bool timedOut    = (await Task.WhenAny(pollTask, timeoutTask) == timeoutTask);

            //timeout - empty return
            if (timedOut)
            {
                return(new LongResponse());
            }

            //send anything that has accumulated
            pushGroup = ClientPlex.GetAndClearItemsToPush(req.SessionKey);
            if (pushGroup != null)
            {
                return(PushGroupToLongResponse(user, pushGroup));
            }

            //nothing to do
            return(new LongResponse());
        }