Esempio n. 1
0
 public void Connect(int userid)
 {
     if (!ConnectedUsers.Contains(userid))
     {
         ConnectedUsers.Add(userid);
     }
 }
Esempio n. 2
0
        public void RemoveUser(User user)
        {
            if (ConnectedUsers.Contains(user))
            {
                ConnectedUsers.Remove(user);

                AddMessage($"{user.Name} left the chat", ChatMessage.ChatMessageTypes.META);
            }
        }
Esempio n. 3
0
        public bool AddUser(User user)
        {
            if (!ConnectedUsers.Contains(user))
            {
                AddMessage($"{user.Name} joined the chat", ChatMessage.ChatMessageTypes.META);

                ConnectedUsers.Add(user);

                return(true);
            }

            return(false);
        }
Esempio n. 4
0
 public Message GetMessage()
 {
     while (!messageQueue.IsEmpty())
     {
         var message = messageQueue.Dequeue();
         //Only return message from someone still connected
         //and that has not timed out
         if (connectedUsers.Contains(message.User) && DateTime.Now.Subtract(message.Date) < TIMEOUT)
         {
             return(message);
         }
     }
     return(null);
 }
Esempio n. 5
0
 private void ClientSubscribed(User user)
 {
     try
     {
         this._currentDispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
         {
             if (!ConnectedUsers.Contains(user))
             {
                 ConnectedUsers.Add(user);
                 ConnectedUsersCount++;
             }
         }));
     }
     catch (Exception exception)
     {
         Logger.Error(exception, OType.FullName, "ClientSubscribed");
     }
 }
 private void E_ReceivedMsg(object sender, string e)
 {
     if (e.StartsWith("@quit"))
     {
         var client = sender as ClientHandler;
         ConnectedUsers.Remove(client.Username); // remove username
     }
     else
     {
         App.Current.Dispatcher.Invoke(() =>
         {
             var client = sender as ClientHandler;
             ReceivedMsg.Add(client.Username + ":" + e);
             RaisePropertyChanged(nameof(CountMsg));
             if (!ConnectedUsers.Contains(client.Username))
             {
                 ConnectedUsers.Add(client.Username);
             }
         });
     }
 }
Esempio n. 7
0
        void WorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Message message = e.UserState as Message;

            if (message != null)
            {
                switch (message.Type)
                {
                case Message.MessageType.Connect:
                {
                    if (message.Username.Equals(Username))
                    {
                        ChatMessages += String.Format("({1}) Welcome, {0}!\n", Username, message.MessageCreationTime.ToString("G"));
                        IsConnected   = true;
                    }
                    else
                    {
                        ConnectedUsers.Add(message.Username);
                        ChatMessages += String.Format("({1}) {0} has joined the server.\n", message.Username, message.MessageCreationTime.ToString("G"));
                    }
                    break;
                }

                case Message.MessageType.Disconnect:
                {
                    if (!message.Username.Equals(Username))
                    {
                        ConnectedUsers.Remove(message.Username);
                        ChatMessages += String.Format("({1}) {0} has left the server.\n", message.Username, message.MessageCreationTime.ToString("G"));
                    }
                    break;
                }

                case Message.MessageType.ChatMessage:
                {
                    if (message.Username != Username && !ConnectedUsers.Contains(message.Username))
                    {
                        ConnectedUsers.Add(message.Username);
                    }

                    ChatMessages += String.Format("({1}) {2}: {0} \n", message.ChatMessage, message.MessageCreationTime.ToString("G"), message.Username);
                    break;
                }

                case Message.MessageType.UsernameAlreadyTaken:
                {
                    ChatMessages += String.Format("({1}) Username {0} is already taken!\n", Username, message.MessageCreationTime.ToString("G"));

                    _networkStream.Close();
                    _tcpClient.Close();
                    _tcpClient = new TcpClient();
                    _worker.CancelAsync();

                    break;
                }

                default:
                    //do nothing
                    break;
                }
            }
        }