Example #1
0
        private void btSendMessage_Click(object sender, EventArgs e)
        {
            string message = DateTime.Now.ToString() + "|   " + tbLogIn.Text + "|     " + tbMessage.Text;

            client.SendMessage(message);
            tbMessage.Text  = "";
            tbMessages.Text = "";
            client.GetMessages();
        }
Example #2
0
        /// <summary>
        /// Occurs when the <see cref="Button"/> is clicked.
        /// </summary>
        private void Button_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            switch (button.Name)
            {
            case "btnConnect":
                ConnectChatClient();
                break;

            case "btnSend":
                chatClient.SendMessage(txtMessage.Text);
                txtMessage.Text = "";
                break;
            }
        }
Example #3
0
        static void ProcessCommand(string command, string[] args)
        {
            switch (command)
            {
            case "join_room":
                var room = args[0];
                client.JoinRoom(room).ContinueWith(t => ProcessResponse(t.Result));
                break;

            case "login":
                var name = args.Length > 0 ? args[0] : "user_" + Guid.NewGuid();
                client.Login(name).ContinueWith(t => ProcessResponse(t.Result));
                break;

            case "send":
                client.SendMessage(args[0], args[1]).ContinueWith(t => ProcessResponse(t.Result));
                break;

            case "list_rooms":
                client.ListRooms().ContinueWith(t => ProcessListRooms(t.Result));
                break;

            case "list_room_users":
                client.ListRoomUsers(args[0]).ContinueWith(t => ProcessListRoomUsers(t.Result));
                break;

            case "sleep":
                var timeout = int.Parse(args[0]);
                Thread.Sleep(timeout);
                break;

            case "stat":
                var stat = client.Stat;
                Console.WriteLine($"CommandsSend: {stat.CommandsSend}");
                Console.WriteLine($"CommandsReceived: {stat.CommandsReceived}");
                Console.WriteLine($"BytesSend: {stat.BytesSend}");
                Console.WriteLine($"BytesReceived: {stat.BytesReceived}");
                break;
            }
        }
Example #4
0
        public static void Main(string[] args)
        {
enterHostName:
            String hostName;
            UInt16 hostPort;

            Console.Write("서버 주소 입력: ");

            // 키를 입력받는다.
            hostName = Console.ReadLine().Trim();

            // null 값이 입력됬다면 (아무것도 입력되지 않았다면)
            if (String.IsNullOrEmpty(hostName))
            {
                Console.WriteLine("다시 입력하세요");
                goto enterHostName;
            }

enterHostPort:
            Console.Write("서버 포트 입력: ");

            try {
                // 포트를 입력받고, UInt16 형으로 변환을 시도한다.
                hostPort = UInt16.Parse(Console.ReadLine().Trim());
            } catch {
                Console.WriteLine("다시 입력하세요");
                goto enterHostPort;
            }

            ChatClient cc = new ChatClient();

            Console.WriteLine("접속 중...");

            cc.ConnectToServer(hostName, hostPort);
            if (!cc.Connected)
            {
                Console.WriteLine("접속 중 오류 발생!");
                Console.Write("아무 키나 누르면 종료합니다.");
                Console.ReadKey(true);
                return;
            }

            // 무한반복
            while (true)
            {
                String msg;
                Console.Write("보낼 메세지 (종료키: X): ");
                msg = Console.ReadLine().Trim();

                // 입력받은 문자열이 null 인 경우, 다시 반복문의 처음으로 돌아간다.
                if (String.IsNullOrEmpty(msg))
                {
                    continue;
                }

                // 입력받은 문자열이 X 인 경우, 프로그램을 종료한다.
                if (msg.Equals("X"))
                {
                    cc.StopClient();
                    return;
                }

                // 그 외의 경우엔 메세지를 보낸다.
                cc.SendMessage(msg);
            }
        }
Example #5
0
        private void InitializeEvents()
        {
            this.ConnectButton.Click += (object sender, EventArgs e) =>
            {
                _chatClient.Connect(NameBox.Text, IpBox.Text, Convert.ToInt32(PortBox.Text)); // <---- Connecting this
            };

            _chatClient.Connected += (endPoint) =>
            {
                MessagesBox.Items.Add("Connected to " + endPoint);
                ConnectButton.Visible     = false;
                DisconnectButton.Visible  = true;
                IpBox.Enabled             = false;
                PortBox.Enabled           = false;
                NameBox.Enabled           = false;
                SendMessageButton.Enabled = true;

                Thread receiveThread = new Thread(new ThreadStart(_chatClient.ReceiveMessage));
                receiveThread.Start();

                _chatClient.SendMessage("connect#" + NameBox.Text);
            };

            _chatClient.ConnectedError += (endPoint) =>
            {
                MessagesBox.Items.Add("Unsuccessful connection to " + endPoint);
                DisconnectButton.Visible  = false;
                ConnectButton.Visible     = true;
                NameBox.Enabled           = true;
                IpBox.Enabled             = true;
                PortBox.Enabled           = true;
                SendMessageButton.Enabled = false;
            };

            this.DisconnectButton.Click += (sender, e) =>
            {
                _chatClient.SendMessage("disconnect#" + NameBox.Text);
                _chatClient.Disconnect(); // <---- Disconnecting this
            };

            _chatClient.Disconnected += () =>
            {
                MessagesBox.Items.Add("Disconnected.");
                DisconnectButton.Visible  = false;
                ConnectButton.Visible     = true;
                NameBox.Enabled           = true;
                IpBox.Enabled             = true;
                PortBox.Enabled           = true;
                SendMessageButton.Enabled = false;
            };

            this.SendMessageButton.Click += (sender, e) =>
            {
                MessagesBox.Items.Add("You:" + SendMessageBox.Text);
                _chatClient.SendMessage("say#" + NameBox.Text + ":" + SendMessageBox.Text); // <---- Send message (1/2)
            };

            this.SendMessageBox.KeyDown += (object sender, KeyEventArgs e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    e.SuppressKeyPress = true;
                    MessagesBox.Items.Add("You:" + SendMessageBox.Text);
                    _chatClient.SendMessage("say#" + NameBox.Text + "#" + SendMessageBox.Text); // <---- Send message (1/2)
                }
            };

            _chatClient.MessageSended += () =>
            {
                SendMessageBox.Text = "";
                SendMessageBox.Select();
            };
        }
Example #6
0
 private void BSendMessageOnClick(object sender, EventArgs e)
 {
     _chatClient.SendMessage(tMessage.Text);
     tMessage.Text = "";
 }