Beispiel #1
0
        private void ReceiverMain()
        {
            try
            {
                for (;;)
                {
                    Message m = PacketIO.Receive(client);                     // wait for msg

                    // Received message!
                    switch ((Commands)m.command)
                    {
                    case Commands.Message:
                        Console.WriteLine(m.username + ": " + m.message);
                        break;

                    case Commands.System:
                        Console.WriteLine("[SYSTEM] " + m.message);
                        break;

                    case Commands.Connection:
                        Console.WriteLine("* " + m.message);
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.Beep();
                Console.WriteLine(e.Message);
            }
        }
Beispiel #2
0
        private void ClientMain()
        {
            Console.WriteLine("Client with ID " + clientID + " thread started");
            callback(new Message(Commands.Connection, username + " has joined", "", 0));

            try
            {
                while (true)
                {
                    Message message = PacketIO.Receive(socket);
                    if (message.command != (byte)Commands.Message)
                    {
                        continue;
                    }
                    string msg = message.message;

                    Console.WriteLine("\t(" + clientID + ") " + username + ": " + msg);

                    if (msg.Length > 1 && msg[0] == '/')
                    {
                        string[] argv    = msg.Substring(1).Split(' ');
                        int      argc    = argv.Count();
                        string   command = argv[0].ToLower();

                        if (command == "setusername" && argc > 1)
                        {
                            this.username = argv[1];
                            PacketIO.Send(socket, new Message(Commands.System, "Username updated", "", 0));
                        }
                        else
                        {
                            PacketIO.Send(socket, new Message(Commands.System, "Command error!", "", 0));
                        }
                    }
                    else
                    {
                        // Broadcast D O N G
                        callback(new Message(msg, username));
                    }
                }
            }
            catch (Exception e)
            {
                Console.Beep();
                Console.WriteLine("Thread " + clientID + ": " + e.Message);
            }

            Console.WriteLine("Client with ID " + clientID + " thread ended");
            callback(new Message(Commands.Connection, username + " has left", "", 0));
        }