Beispiel #1
0
        public async Task StartReceive()
        {
            while (WebSocket.State == WebSocketState.Open)
            {
                IsActive = true;
                try
                {
                    var message = await GetMessage();

                    if (message == null)
                    {
                        IsActive = false;
                        ClientDisconnected?.Invoke(this, "Socket Disconnected");
                    }
                    else
                    {
                        MessageReceived?.Invoke(this, message);
                    }
                }
                catch (Exception ex)
                {
                    LogEngine.Error(ex);
                    IsActive = false;
                    ClientDisconnected?.Invoke(this, "Socket Disconnected");
                    break;
                }
            }
        }
Beispiel #2
0
        private async Task <bool> TryConnect()
        {
            try
            {
                var tcp = new ClientWebSocket();
                await tcp.ConnectAsync(new Uri(Address), new CancellationToken());

                DisposeSocket();
                SocketHandler = new SocketHandler(tcp);
                SocketHandler.MessageReceived    += SocketHandler_MessageReceived;
                SocketHandler.ClientDisconnected += SocketHandler_ClientDisconnected;
                Recieve();
                return(true);
            }
            catch (Exception ex)
            {
                LogEngine.Error(ex);
                return(false);
            }
        }