// This is called when we want to raise the StatusChanged event
 public static void OnStatusChanged(StatusChangedEventArgs e)
 {
     StatusChangedEventHandler statusHandler = StatusChanged;
     if (statusHandler != null)
     {
         // Invoke the delegate
         statusHandler(null, e);
     }
 }
        // Send messages from one user to all the others
        public static void SendMessage(string From, string Message)
        {
            StreamWriter swSenderSender;

            // First of all, show in our application who says what
            e = new StatusChangedEventArgs(From + ": " + Message);
            OnStatusChanged(e);

            // Create an array of TCP clients, the size of the number of users we have
            TcpClient[] tcpClients = new TcpClient[ChatServer.htUsers.Count];
            // Copy the TcpClient objects into the array
            ChatServer.htUsers.Values.CopyTo(tcpClients, 0);
            // Loop through the list of TCP clients
            for (int i = 0; i < tcpClients.Length; i++)
            {
                // Try sending a message to each
                try
                {
                    // If the message is blank or the connection is null, break out
                    if (Message.Trim() == "" || tcpClients[i] == null)
                    {
                        continue;
                    }
                    // Send the message to the current user in the loop
                    swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                    swSenderSender.WriteLine(From + ": " + Message);
                    swSenderSender.Flush();
                    swSenderSender = null;
                }
                catch // If there was a problem, the user is not there anymore, remove him
                {
                    RemoveUser(tcpClients[i]);
                }
            }
        }
示例#3
0
 public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
 {
     this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
 }