Ejemplo n.º 1
0
 /// <summary>
 ///     Queues an Identify Message.
 /// </summary>
 /// <returns>Task</returns>
 private Task _queueIdentifyAsync()
 => Gateway.PerformIdentifyAsync(_identifyAsync, Id);
Ejemplo n.º 2
0
        /// <summary>
        ///     Connects this Shard to the Discord Gateway.
        /// </summary>
        /// <returns>Task</returns>
        public async Task ConnectAsync()
        {
            if (!Gateway.Ready)
            {
                await Gateway.InitializeAsync();
            }

            LastHeartbeatAcked = true;

            if (WebSocketClient != null)
            {
                _log(LogLevel.DEBUG, "Disposing old WebSocketClient...");
                WebSocketClient.Open    -= _onOpen;
                WebSocketClient.Message -= _onMessage;
                WebSocketClient.Close   -= _onClose;
                WebSocketClient.Error   -= _onError;
                WebSocketClient.Dispose();
                _stopHeartbeatTimer();
            }

            WebSocketClient = new WebSocketClient(Gateway.Url);

            WebSocketClient.Open    += _onOpen;
            WebSocketClient.Message += _onMessage;
            WebSocketClient.Close   += _onClose;
            WebSocketClient.Error   += _onError;


            _log(LogLevel.DEBUG, "Connecting to Websocket...");
            try
            {
                await WebSocketClient.ConnectAsync();
            }
            catch (Exception)
            {
                int delay;
                switch (ReconnectStrategy)
                {
                case ReconnectStrategy.FIBONACCI:
                    delay = Util.Fibonacci(ReconnectValue);
                    ReconnectValue++;
                    break;

                case ReconnectStrategy.EXPONENTIAL:
                    delay           = ReconnectValue;
                    ReconnectValue *= 2;
                    break;

                case ReconnectStrategy.PERIODICALLY:
                    delay = ReconnectValue;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                await Task.Delay(delay);
                await ConnectAsync();

                return;
            }

            await _waitForIdentify();
        }