Exemple #1
0
        public void Start()
        {
            try
            {
                _listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 10000);
                _listener.Start();
                Console.WriteLine("Server up");

                while (true)
                {
                    var acceptTcpClient = _listener.AcceptTcpClient();

                    var chatClient = new ChatClient(Guid.NewGuid(), acceptTcpClient, new MessageCache(), new ClientActions()
                    {
                        OnDisconnect     = (id) => _clientPool.Remove(id),
                        OnMessageCreated = OnMessageCreated
                    });

                    _clientPool.Add(chatClient);

                    Console.WriteLine("{0} connected to server", chatClient.Id);
                    Task.Run(() => chatClient.ReadLoop());
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
                throw;
            }
            finally
            {
                _listener.Stop();
            }
        }