Пример #1
0
        public void listenNewMessage()
        {
            while (true)
            {
                TcpMessage msg = listenMsgObject();
                if (msg == null)
                {
                    break;
                }
                switch (msg.getType())
                {
                case TcpMessage.TypeMsg.Msg:
                    chatForm.printMessageChat(msg.getLogin() + ":" + msg.getMsg());
                    if (serverListener != null)
                    {
                        serverListener.sendAnotherUsersMessage(msg, userInfo.getTcpClient());
                    }
                    break;

                case TcpMessage.TypeMsg.Logout:
                    //TODO: implement this
                    if (serverListener != null)
                    {
                        chatForm.removeUserToListByName(msg.getLogin());
                        TcpClient client;
                        serverListener.mapUsers.TryGetValue(msg.getLogin(), out client);
                        if (client != null)
                        {
                            serverListener.mapUsers.Remove(msg.getLogin());
                            client.Close();
                        }
                        serverListener.sendAnotherUsersMessage(new TcpMessage(TcpMessage.TypeMsg.Msg, "User with login '" + msg.getLogin() + "' has exited!", ""), null);
                    }
                    break;

                default:
                    chatForm.printError(msg);
                    break;
                }
            }
        }
Пример #2
0
 public void listenNewUser()
 {
     while (true)
     {
         TcpClient  tcpClient = Listener.AcceptTcpClient();
         TcpMessage msg       = getTcpMessage(tcpClient);
         if (msg.getType().Equals(TcpMessage.TypeMsg.Login))
         {
             UserChat userChat         = new UserChat(new UserInfo(msg.getLogin(), tcpClient), chatForm, this);
             Thread   backgroundThread = new Thread(new ThreadStart(userChat.listenNewMessage));
             backgroundThread.Start();
             updateUsersList(userChat);
         }
         else
         {
             chatForm.printError(msg);
         }
     }
 }