public void BroadcastMessage(int id, string message) { // client disconnected if (message == null) { ChatClientList.Remove(ChatClientList.Single(x => x.Id == id)); message = string.Format("Client {0} disconnected", id); Console.WriteLine(message); id = 0; } foreach (var client in ChatClientList) { SendMessage(id, client, message); } }
private void AcceptConnectionRequest() { // create a new connection var tcpClient = TcpListener.AcceptTcpClient(); int newId = ++LastId; var newChatClient = new ChatClient { Id = newId, TcpClient = tcpClient }; SendMessage(0, newChatClient, string.Format("You are now connected. Your id is: {0}", newId)); BroadcastMessage(0, string.Format("Client {0} connected", newId)); ChatClientList.Add(newChatClient); // create a client thread to communicate new ClientThread(this, newChatClient); Console.WriteLine("New client connected. Id = {0}", newId); }