예제 #1
0
        public async Task <IActionResult> List()
        {
            if (!await _client.ConnectAsync(new IPEndPoint(_address, _serverPort)))
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, "Failed to connect to the server."));
            }

            await _client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.Connect} {ClientType.Short.ToString()}{Package.Terminator}"));

            while (true)
            {
                var p = await _client.ReceiveAsync();

                if (p == null)
                {
                    return(StatusCode((int)HttpStatusCode.InternalServerError, "Connection dropped."));
                }

                switch (p.Key)
                {
                case CommandKey.Connected:
                    await _client.SendAsync(Encoding.UTF8.GetBytes($"{CommandKey.ListClient}{Package.Terminator}"));

                    break;

                default:
                    var clients = JsonConvert.DeserializeObject <List <ClientInfo> >(p.Content).OrderByDescending(n => n.ConnectTime);
                    await _client.CloseAsync();

                    return(Ok(clients));
                }
            }
        }
예제 #2
0
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            _shouldReconnect = false;
            _checkPingTimer?.Change(Timeout.Infinite, 0);
            _pingTimer?.Change(Timeout.Infinite, 0);
            await _client.CloseAsync();

            _logger.LogInformation("Background service is stopped...");
        }
        public override async ValueTask DisconnectAsync()
        {
            try
            {
                _cts.Cancel();
                await _client.CloseAsync();

                OnDisconnect?.Invoke(this, new DisconnectEventArgs()
                {
                    Id = Id,
                    ConnectionAddress = ConnectionAddress
                });
            }
            finally
            {
                IsConnected = false;
            }
        }
예제 #4
0
        public override async Task CloseAsync()
        {
            await _client.CloseAsync();

            _cancellationTokenSource.Cancel();
        }