Exemplo n.º 1
0
        private void DoWork(CancellationToken token)
        {
            try
            {
                listenerStoped.Reset();
                while (!token.IsCancellationRequested)
                {
                    TcpClient client = server.AcceptTcpClient();

                    Task.Run((() =>
                    {
                        ClientConnect chatClient = new ClientConnect
                        {
                            Client = client
                        };
                        chatClient.CommandRecived += ChatClient_MessageRecived;
                        chatClient.ClientConnected += ChatClient_ClientConnected;
                        chatClient.ClientDisconnected += ChatClient_ClientDisconected;

                        chatClient.Listen(true);
                    }));
                }
            }
            catch (Exception ex)
            {
                //ConsoleOutput.WriteLineError($"Ошибка работы сервера: '{ex}'.");
            }
            finally
            {
                clients.ForEach(x => x.Dispose());
                clients.Clear();
                server?.Stop();
                listenerStoped.Set();
            }
        }