public void Disconnect()
 {
     try
     {
         SocketConnection.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, _cancellationToken);
     }
     catch (Exception ex)
     {
         logger.Warn(ex, $"{Address}: Error while closing agent connection: {ex.Message}");
     }
 }
        private async void StartListen()
        {
            byte[] buffer = new byte[ReceiveChunkSize];

            try
            {
                while (SocketConnection.State == WebSocketState.Open)
                {
                    var stringResult = new StringBuilder();

                    WebSocketReceiveResult result;
                    do
                    {
                        result = await SocketConnection.ReceiveAsync(new ArraySegment <byte>(buffer), _cancellationToken);

                        if (result.MessageType == WebSocketMessageType.Close)
                        {
                            await
                            SocketConnection.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);

                            OnClose();
                        }
                        else
                        {
                            var str = Encoding.UTF8.GetString(buffer, 0, result.Count);
                            stringResult.Append(str);
                        }
                    } while (!result.EndOfMessage);

                    OnMessage(stringResult.ToString());
                }
            }
            catch (Exception ex)
            {
                OnClose();
                OnConnectionError(ex.Message);
            }
            finally
            {
                SocketConnection.Dispose();
            }
        }
Beispiel #3
0
        private async Task DisconnectAsync()
        {
            if (connection != null)
            {
                try
                {
                    dispatcherTimer.Stop();
                    GamepadService.Autopiloting = false;

                    await connection.SendAsync("Stop");

                    await connection.CloseAsync();
                }
                catch { }

                connection = null;

                RoverAddressTextBox.IsEnabled = true;
                connectButton.Visibility      = Visibility.Visible;
                disconnectButton.Visibility   = Visibility.Collapsed;
                roverControl.Visibility       = Visibility.Collapsed;
            }
        }