Пример #1
0
        public IMessage GetMessage()
        {
            StreamReader reader  = new StreamReader(stream);
            string       message = reader.ReadLine();
            IMessage     Msg     = MsgPareser.Parse(message);

            return(Msg);
        }
Пример #2
0
        //принимает 1 сообщение от пользователя
        public string GetMessage(MyTCPChatUser user)
        {
            //MyTCPClient client = listener.AcceptTCPClient();
            //var client = listener.AcceptTcpClient();
            NetworkStream stream  = user.stream;
            StreamReader  reader  = new StreamReader(stream);
            string        message = reader.ReadLine();
            IMessage      Msg     = MsgPareser.Parse(message);

            if (Msg is AllMessage A)
            {
                A = (AllMessage)Msg;
                SendToAll(A);
                return(A.ToOutString());
            }
            else if (Msg is PersonalMessage P)
            {
                P = (PersonalMessage)Msg;
                SendTo(P);
                return(P.ToOutString());
            }
            else if (Msg is HelloMessage H)
            {
                H = (HelloMessage)Msg;
                //отсылаем всем что новый пользователь подключился
                AllMessage all = new AllMessage()
                {
                    Name = H.Name, Content = H.Content
                };
                SendToAll(all);
                //добавляем клиента и отсылаем обратно ID
                AddConnection(user, H);
                //Рассылаем всем новый лист клиентов
                SendList();
                return(H.ToOutString());
            }
            else if (Msg is GoodbyMessage G)
            {
                G = (GoodbyMessage)Msg;
                //удаляем пользователя
                RemoveConnection(user, G);
                //рассылаем всем что пользователь вышел
                AllMessage all = new AllMessage()
                {
                    Name = G.Name, Content = G.Content
                };
                SendToAll(all);
                //Рассылаем всем новый лист клиентов
                SendList();
                return(G.ToOutString());
            }
            else
            {
                throw new Exception("Recieve message has an invalid type");
            }
        }