public async Task <string> Receive(CancellationToken cancellationToken)
        {
            try
            {
                return(await m_Underlying.Receive(cancellationToken));
            }
            catch (WebSocketException wse)
            {
                Trace.TraceError(wse.ToString());
            }

            for (int i = 0; i < 5; i++)
            {
                TryDisposeOld();
                await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);

                Trace.WriteLine("Caught websocket exception .. reconnecting");
                try
                {
                    m_Underlying = await m_Factory();
                }
                catch (Exception e)
                {
                    Trace.TraceError("Failed to reconnect .. " + e);
                }
            }

            if (m_Underlying == null)
            {
                throw new Exception("Failed to reconnect to Slack after several tries");
            }

            return(await m_Underlying.Receive(cancellationToken));
        }
Exemplo n.º 2
0
        private static void MainLoop(ISlackRealTimeMessaging slackRtm, SlackMessageHandler handler, SlackApi slackApi, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                var nextMessage = slackRtm.Receive(cancellationToken);
                while (!nextMessage.Wait(TimeSpan.FromSeconds(10)))
                {
                    var tickResult = handler.HandleTimerTick();
                    Console.Write(".");
                    DoResponse(slackApi, tickResult, cancellationToken);
                }

                var messageResult = handler.Handle(nextMessage.Result);
                DoResponse(slackApi, messageResult, cancellationToken);
            }
        }