Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async ValueTask ConnectAsync()
        {
            if (_webSocket.State == WebSocketState.Open)
            {
                throw new InvalidOperationException(
                          $"WebSocket is not in open state. Current state: {_webSocket.State}");
            }

            await _webSocket.ConnectAsync(Host, _connectionTokenSource.Token)
            .ContinueWith(async task => {
                await task;
                IsConnected = true;

                _connectionTokenSource = new CancellationTokenSource();
                await Task.WhenAll(OnOpenAsync.Invoke(), ReceiveAsync(), SendAsync());
            });
        }
Пример #2
0
        /// <summary>
        /// Initializes instance of <see cref="ClientWebSocket"/> and connects to server.
        /// </summary>
        public async Task ConnectAsync()
        {
            if (_webSocket.State == WebSocketState.Open)
            {
                throw new InvalidOperationException(
                          $"WebSocket is already in open state. Current state: {_webSocket.State}");
            }

            async Task VerifyConnectionAsync(Task task)
            {
                if (task.Exception != null)
                {
                    await OnErrorAsync.Invoke(task.Exception);
                    await RetryConnectionAsync();

                    return;
                }

                _isConnected        = true;
                _connectionAttempts = 0;
                _reconnectInterval  = TimeSpan.Zero;
                _cancellationToken  = new CancellationTokenSource();
                await OnOpenAsync.Invoke();

                await ReceiveAsync();
            }

            try {
                await _webSocket
                .ConnectAsync(_url, CancellationToken.None)
                .ContinueWith(VerifyConnectionAsync);
            }
            catch (Exception exception) {
                if (!(exception is ObjectDisposedException))
                {
                    return;
                }

                await RetryConnectionAsync();
            }
        }
Пример #3
0
 Task IOpenCloseEventCaller.OpenAsync()
 => OnOpenAsync.InvokeAllAsync(this, EventArgs.Empty);
Пример #4
0
 Task IPromptEventCaller <T> .OpenAsync(T prompt)
 => OnOpenAsync.InvokeAllAsync(this, new(prompt));
Пример #5
0
 /// <summary>
 /// A method to create a modal.
 /// </summary>
 /// <param name="modalModel">A instance of <see cref="ModalModel"/>.</param>
 /// <returns>An instance of <see cref="Task"/>.</returns>
 public async Task CreateAsync(ModalModel modalModel)
 {
     ModalModel = modalModel;
     await OnOpenAsync.Invoke();
 }