Ejemplo n.º 1
0
        /// <summary>
        /// Lädt die ChatHistory vom Server und zeigt sie auf der Form an.
        /// </summary>
        public void LoadChat()
        {
            Message sendMessage = new Message()
            {
                content = new Content(ContentType.RequestChat)
            };

            streamRW.WriteLine(sendMessage.ToString());

            List <Message> chatHistory = Serializer.Deserialize <List <Message> >(streamRW.ReadLine());

            if (chatHistory != null)
            {
                foreach (Message message in chatHistory)
                {
                    System.Windows.HorizontalAlignment horizontallignment;
                    if (message.sender == username)
                    {
                        horizontallignment = System.Windows.HorizontalAlignment.Right;
                    }
                    else
                    {
                        horizontallignment = System.Windows.HorizontalAlignment.Left;
                    }

                    RaiseMessageReceivedEvent(horizontallignment, message.content.parameter[0], message.sender, message.sendTime);
                }
            }

            dTimer          = new DispatcherTimer();
            dTimer.Interval = new TimeSpan(0, 0, 1);
            dTimer.Tick    += DTimer_Tick;
            dTimer.Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verarbeitet Nachrichten, die an den Server von der Verbindung gesendet werden.
        /// </summary>
        /// <param name="Message">Aufbau: Befehle:Parameter</param>
        /// <returns></returns>
        void ProcessMessage(string Content)
        {
            Message receivedMessage = Serializer.Deserialize <Message>(Content);
            Command command         = (Command)receivedMessage.content;

            switch (command.type)
            {
            case CommandType.Login:
                StreamRW streamRW = new StreamRW(tcpClient.GetStream());
                if (command.parameter[0] == command.parameter[1])
                {
                    streamRW.WriteLine("Login successfull");
                    username = command.parameter[0];
                    Log.WriteLine("[Server][{0}]{1} logged in", DateTime.Now, username);
                }
                else
                {
                    streamRW.WriteLine("Login failed");
                    Log.WriteLine("[Server][{0}{1}] failed to log in", DateTime.Now, username);
                }
                break;

            case CommandType.Message:
                Log.WriteLine("[Server][{0}]{1}: {2}", receivedMessage.sendTime, receivedMessage.sender, command.parameter[0]);
                break;

            case CommandType.Disconnect:
                Log.WriteLine("[Server][{0}]{1}: Disconnected: {2}", receivedMessage.sendTime, receivedMessage.sender, command.parameter[0]);
                break;
            }
        }
Ejemplo n.º 3
0
 public void Close(string Reason)
 {
     streamRW.WriteLine(new Message()
     {
         content  = new Content(ContentType.Kicked, Reason),
         sendTime = DateTime.Now
     }.ToString());
     tcpClient.Close();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Verarbeitet Nachrichten, die an den Server von der Verbindung gesendet werden.
        /// </summary>
        /// <param name="Message">Aufbau: Befehle:Parameter</param>
        /// <returns></returns>
        void ProcessMessage(string Content)
        {
            // Deserialisiert die Message.
            Message receivedMessage = Serializer.Deserialize <Message>(Content);
            Content content         = receivedMessage.content;

            // Verarbeitet die Nachricht.
            switch (content.type)
            {
            // Registriert einen Benutzer in der UserDatabase und meldet das Ergebnis.
            case ContentType.Register:
                if (Server.userDatabase.Register(content.parameter[0], content.parameter[1]))
                {
                    this.streamRW.WriteLine("Register succesfull");
                    Log.WriteLine("[Server][{0}] {1} registered.", DateTime.Now, content.parameter[0]);
                }
                else
                {
                    Log.WriteLine("[Server][{0}] {1} failed to register.", DateTime.Now, content.parameter[0]);
                }
                break;

            // Überprüft die Anmeldedaten und gibt das Ergebnis zurück.
            case ContentType.Login:
                StreamRW streamRW = new StreamRW(tcpClient.GetStream());
                if (Server.userDatabase.Login(content.parameter[0], content.parameter[1]))
                {
                    streamRW.WriteLine("Login successfull");
                    username = content.parameter[0];
                    Log.WriteLine("[Server][{0}] {1} logged in", DateTime.Now, username);
                }
                else
                {
                    streamRW.WriteLine("Login failed");
                    Log.WriteLine("[Server][{0}] {1} failed to log in", DateTime.Now, username);
                }
                break;

            case ContentType.Disconnect:
                Server.connectedUser.Remove(this);
                tcpClient.Close();
                Log.WriteLine("[Server][{0}] {1}: Disconnected: {2}", receivedMessage.sendTime, receivedMessage.sender, content.parameter[0]);
                break;

            case ContentType.RequestChat:
                this.streamRW.WriteLine(Serializer.Serialize <List <Message> >(Server.chatHistory, false).Replace(Environment.NewLine, ""));
                break;

            case ContentType.Message:
                Server.chatHistory.Add(receivedMessage);
                foreach (Connection connection in Server.connectedUser)
                {
                    connection.streamRW.WriteLine(new Message()
                    {
                        content  = receivedMessage.content,
                        sender   = receivedMessage.sender,
                        sendTime = receivedMessage.sendTime
                    }.ToString());
                }
                Log.WriteLine("[Server][{0}] {1}: {2}", receivedMessage.sendTime, receivedMessage.sender, content.parameter[0]);
                break;
            }
        }
Ejemplo n.º 5
0
        public bool Send(Message message)
        {
            streamRW.WriteLine(message.ToString());

            return(true);
        }