Пример #1
0
 public static void SendAdminMessage(string Message)
 {
     StreamWriter swSenderSender;
     e = new StatusChangedEventArgs(GetTime() + "|" + "Server: " + Message);
     OnStatusChanged(e);
    TcpClient[] tcpClients = new TcpClient[ChatServer.htUsers.Count];
     ChatServer.htUsers.Values.CopyTo(tcpClients, 0);
     for (int i = 0; i < tcpClients.Length; i++)
     {
         try
         {
             if (Message.Trim() == "" || tcpClients[i] == null)
             {
                 continue;
             }
              {
                 swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                 swSenderSender.WriteLine(GetTime() + "|" + "Server: " + Message);
                 swSenderSender.Flush();
                 swSenderSender = null;
             }
         }
         catch 
         {
             RemoveUser(tcpClients[i]);
         }
     }
 }
Пример #2
0
 public static void OnStatusChanged(StatusChangedEventArgs e)
 {
     StatusChangedEventHandler statusHandler = StatusChanged;
     if (statusHandler != null)
     {
         statusHandler(null, e);
     }
 }
Пример #3
0
 public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
 {
     // Call the method that updates the form
     try
     {
         this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
     }
     catch
     {
         //MessageBox.Show("Disconnessione!");
     }
 }
Пример #4
0
 //Occurs when a new client is accepted
 private void AcceptClient(object newClient)
 {
     //Set the argument to a message
     StatusChangedEventArgs evArg = new StatusChangedEventArgs("A client just connected");
     //Fire the event because a new client was accepted
     StatusChangedEvent(this, evArg);
 }
Пример #5
0
 public void myMono_StatusChanged(object sender, StatusChangedEventArgs e)
 {
     ChatBox.AppendText("Monitor message : " + e.EventMessage);
 }
Пример #6
0
 public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
 {
     this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
 }
Пример #7
0
 public static void SendMessage(string From, string Message)
 {
     StreamWriter swSenderSender;
     string[] To = Message.Split('|');
     e = new StatusChangedEventArgs(GetTime()+"|"+From + " : " + Message);
     OnStatusChanged(e);
     string[] StringMessage = new string[ChatServer.htUsers.Count];
     ChatServer.htConnections.Values.CopyTo(StringMessage, 0);
   
     TcpClient[] tcpClients = new TcpClient[ChatServer.htUsers.Count];
     ChatServer.htUsers.Values.CopyTo(tcpClients, 0);
     for (int i = 0; i < tcpClients.Length; i++)
     {
         try
         {
             if (Message.Trim() == "" || tcpClients[i] == null)
             {
                 continue;
             }
             if (Message.Contains("GetUsers"))
             {
                 swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                 swSenderSender.WriteLine("Users|" + String.Join("|", StringMessage));
                 swSenderSender.Flush();
                 swSenderSender = null;
                
             }
             else
             if (To[0] == "All" || To[0].ToLower().Trim() == StringMessage[i].ToLower().Trim() || From.ToLower().Trim() == StringMessage[i].ToLower().Trim())
             {
                 swSenderSender = new StreamWriter(tcpClients[i].GetStream());
                 swSenderSender.WriteLine(GetTime() + "|" + From + ": " + To[1]);
                 swSenderSender.Flush();
                 swSenderSender = null;
             }
         }
         catch 
         {
             RemoveUser(tcpClients[i]);
         }
     }
 }
Пример #8
0
        // Send messages from one user to all the others
        public void SendMessage(string From, string Message)
        {
            StreamWriter swSenderSender;

            // First of all, show in our application who says what
            e = new StatusChangedEventArgs(From + " says: " + 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 + " says: " + Message);
                    swSenderSender.Flush();
                    swSenderSender = null;
                }
                catch // If there was a problem, the user is not there anymore, remove him
                {
                    RemoveUser(tcpClients[i]);
                    num_client--;
                }
            }
        }
Пример #9
0
 // 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);
     }
 }