private void btnSend_Click(object sender, EventArgs e)
        {
            if (gbChatRoom.Enabled && btnSend.Enabled &&
                m_Client != null && m_Client.GetState() > 0 &&
                m_UserName.Length > 0 && m_UserToken.Length > 0 &&
                m_RoomName.Length > 0)
            {
                string words = edtChat.Text;
                if (words != null && words.Length > 0)
                {
                    btnSend.Enabled = false;
                    try
                    {
                        edtChat.Text = "";

                        SendMessageRequest request = new SendMessageRequest();
                        request.UserName  = m_UserName;
                        request.UserToken = m_UserToken;
                        request.RoomName  = m_RoomName;
                        request.IsPrivate = ckbPrivate.Checked;
                        request.Target    = cbbChatToWho.SelectedIndex > 0 ? cbbChatToWho.Items[cbbChatToWho.SelectedIndex].ToString() : "";
                        request.Words     = words;

                        m_Client.SendString("room/chat/" + WebMessage.ToJsonString <SendMessageRequest>(request));
                    }
                    finally
                    {
                        btnSend.Enabled = true;
                    }
                }
            }
        }
        private void btnEnterRoom_Click(object sender, EventArgs e)
        {
            if (!gbLobby.Enabled || !btnEnterRoom.Enabled)
            {
                return;
            }
            if (listRooms.SelectedIndex >= 0 && m_UserName.Length > 0 && m_UserToken.Length > 0)
            {
                string roomName = listRooms.Items[listRooms.SelectedIndex].ToString();

                if (roomName != null && roomName.Length > 0 && roomName.Contains('('))
                {
                    roomName = roomName.Substring(0, roomName.IndexOf('(')).Trim();
                }

                if (roomName != null && roomName.Length > 0)
                {
                    EnterRoomRequest request = new EnterRoomRequest();
                    request.UserName  = m_UserName;
                    request.UserToken = m_UserToken;
                    request.RoomName  = roomName;

                    m_Client.SendString("room/enter/" + WebMessage.ToJsonString <EnterRoomRequest>(request));
                }
            }
        }
        private void btnLogout_Click(object sender, EventArgs e)
        {
            string requestUrl = edtLoginUrl.Text;

            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl);

            httpWebRequest.ContentType = "text/plain";
            httpWebRequest.Method      = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                LogoutRequest request = new LogoutRequest();
                request.UserName  = m_UserName;
                request.UserToken = m_UserToken;

                string json = WebMessage.ToJsonString <LogoutRequest>(request);

                streamWriter.Write("login/logout/" + json);
                streamWriter.Flush();

                streamWriter.Close();
            }

            string          responseJson = "";
            HttpWebResponse response     = (HttpWebResponse)httpWebRequest.GetResponse();

            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                responseJson = sr.ReadToEnd();

                LogMsg(responseJson);
            }
        }
        private void timerRefreshRooms_Tick(object sender, EventArgs e)
        {
            if (m_Client != null && m_Client.GetState() > 0 &&
                m_UserName.Length > 0 && m_UserToken.Length > 0)
            {
                GetRoomListRequest request = new GetRoomListRequest();
                request.UserName  = m_UserName;
                request.UserToken = m_UserToken;

                m_Client.SendString("lobby/list/" + WebMessage.ToJsonString <GetRoomListRequest>(request));
            }
        }
        public void OnConnect(Session session)
        {
            LogMsg("Client Connected, ID: " + session.GetId());

            if (m_UserName.Length > 0 && m_UserToken.Length > 0)
            {
                EnterLobbyRequest request = new EnterLobbyRequest();
                request.UserName  = m_UserName;
                request.UserToken = m_UserToken;

                m_Client.SendString("lobby/enter/" + WebMessage.ToJsonString <EnterLobbyRequest>(request));
            }
        }
        private void btnLeave_Click(object sender, EventArgs e)
        {
            if (gbChatRoom.Enabled && btnSend.Enabled &&
                m_Client != null && m_Client.GetState() > 0 &&
                m_UserName.Length > 0 && m_UserToken.Length > 0 &&
                m_RoomName.Length > 0)
            {
                ExitRoomRequest request = new ExitRoomRequest();
                request.UserName  = m_UserName;
                request.UserToken = m_UserToken;
                request.RoomName  = m_RoomName;

                m_Client.SendString("room/exit/" + WebMessage.ToJsonString <ExitRoomRequest>(request));
            }
        }
        private void btnCreateRoom_Click(object sender, EventArgs e)
        {
            string newRoomName = "";

            if (InputBox("Create New Chat Room", "New Room Name", ref newRoomName) == DialogResult.OK)
            {
                if (m_UserName.Length > 0 && m_UserToken.Length > 0)
                {
                    CreateRoomRequest request = new CreateRoomRequest();
                    request.UserName  = m_UserName;
                    request.UserToken = m_UserToken;
                    request.RoomName  = newRoomName;

                    m_Client.SendString("room/create/" + WebMessage.ToJsonString <CreateRoomRequest>(request));
                }
            }
        }
        public void EnterLobby(string result)
        {
            LogMsg(result);

            Invoke((Action)(() =>
            {
                if (result.ToLower() == "ok")
                {
                    gbLogin.Enabled = false;

                    if (m_RoomName == null || m_RoomName.Length <= 0)
                    {
                        gbLobby.Enabled = true;
                        m_UserList.Clear();
                        UpdateUserList();
                        richChatBox.Clear();
                    }

                    if (m_UserName.Length > 0 && m_UserToken.Length > 0 && m_RoomName.Length > 0)
                    {
                        gbChatRoom.Enabled = true;
                        ShowText("Re-enter lobby/chat-room successfully");
                    }

                    if (m_UserName.Length > 0 && m_UserToken.Length > 0)
                    {
                        GetRoomListRequest request = new GetRoomListRequest();
                        request.UserName = m_UserName;
                        request.UserToken = m_UserToken;

                        m_Client.SendString("lobby/list/" + WebMessage.ToJsonString <GetRoomListRequest>(request));
                    }
                }
                else
                {
                    ShowText("ERROR: " + "Failed to enter lobby: " + result);
                }
            }));
        }
        private string Login(string loginUrl, string userName, string password = "", string userToken = "")
        {
            string errMsg = "Invalid input";

            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(loginUrl);
                httpWebRequest.ContentType = "text/plain";
                httpWebRequest.Method      = "POST";
                httpWebRequest.KeepAlive   = false;

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    LoginRequest request = new LoginRequest();
                    request.UserName  = userName;
                    request.Password  = password;
                    request.UserToken = userToken;

                    string json = WebMessage.ToJsonString <LoginRequest>(request);

                    streamWriter.Write("login/login/" + json);
                    streamWriter.Flush();

                    //streamWriter.Close();
                }

                string          responseJson = "";
                HttpWebResponse response     = (HttpWebResponse)httpWebRequest.GetResponse();
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    responseJson = sr.ReadToEnd();

                    LogMsg(responseJson);
                }

                errMsg = "Abnormal reply";
                if (responseJson != null && responseJson.Length > 0)
                {
                    LoginReply reply = WebMessage.ToJsonObject <LoginReply>(responseJson);
                    if (reply != null && reply.Result != null)
                    {
                        if (reply.Result.ToLower() == "ok")
                        {
                            m_UserName  = userName;
                            m_UserToken = reply.UserToken;

                            m_Client.Open((m_Client.HasValidationCallback() ? "wss://" : "ws://") + reply.ServerUri);

                            return("");
                        }

                        errMsg = reply.Result;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }

            return(errMsg);
        }