Пример #1
0
        static void Main(string[] args)
        {
            Console.WindowWidth = 80;
            Console.Title       = "SmallChat - by Valentino Giudice";

            string chatId;

            while ((chatId = ConsoleInput("Chat ID")) == "")
            {
                ;
            }
            string nick;

            while ((nick = ConsoleInput("Nickname")) == "")
            {
                ;
            }
            string    key = ConsoleInput("Key", hidden: true);
            IPAddress broadcast;

            while (!IPAddress.TryParse(ConsoleInput("Broadcast address", "255.255.255.255"), out broadcast) || broadcast.Equals(IPAddress.Any))
            {
                ;
            }

            SCHost myself = new SCHost(nick, chatId, SCEDA.Digest(Encoding.ASCII.GetBytes(key)), broadcast);

            myself.OnReceive += myself_OnReceive;
            myself.OnWelcome += myself_OnWelcome;
            myself.OnHello   += myself_OnHello;
            myself.OnLeave   += myself_OnLeave;
            myself.OnConflictNotification  += myself_OnConflictNotification;
            myself.OnMalformedReceived     += myself_OnMalformed;
            myself.OnMalformedNotification += myself_OnMalformed;

            Console.CancelKeyPress += delegate
            {
                myself.Dispose();
            };

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Begin to chat!\n");
            Console.ResetColor();
            char input;

            for (; ;)
            {
                string msg = "";
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write(nick + ": ");
                Console.ResetColor();
                while (((input = Console.ReadKey().KeyChar) != '\r') && msg.Length + nick.Length + 2 < Console.WindowWidth - 1)
                {
                    if (input == '\0')
                    {
                        Console.Write('\b');
                    }
                    else
                    {
                        if (input == '\b')
                        {
                            if (Console.CursorLeft < nick.Length + 2)
                            {
                                Console.SetCursorPosition(0, Console.CursorTop);
                                Console.ForegroundColor = ConsoleColor.Magenta;
                                Console.Write(nick + ": ");
                                Console.ResetColor();
                            }
                            else
                            {
                                Console.Write(" \b");
                                msg = msg.Remove(msg.Length - 1);
                            }
                        }
                        else
                        {
                            msg += input;
                        }
                    }
                }
                if (msg.Length + nick.Length + 2 < Console.WindowWidth - 1)
                {
                    Console.WriteLine();
                }
                myself.Send(msg);
            }
        }