Beispiel #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());
            });
        }
Beispiel #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();
            }
        }
Beispiel #3
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();
 }