예제 #1
0
        private void btnRegist_Click(object sender, EventArgs e)
        {
            if (WebSocketManager.IsConnected() == false)
            {
                WebSocketManager.InitWebSocket();
                if (WebSocketManager.IsConnected() == false)
                {
                    string tips = string.Format("无法连接服务器,请确保服务器地址为{0},且处于开启状态", AppValues.SERVER_URL);
                    MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            string inputUsername = txtUsername.Text.Trim();
            string inputPassword = txtPassword.Text.Trim();

            if (string.IsNullOrEmpty(inputUsername))
            {
                MessageBox.Show("用户名不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(inputPassword))
            {
                MessageBox.Show("密码不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            RegistRequest.Builder builder = RegistRequest.CreateBuilder();
            builder.SetUsername(inputUsername);
            builder.SetPassword(inputPassword);
            WebSocketManager.SendMessage(RpcNameEnum.Regist, builder.Build().ToByteArray(), OnRegistCallback);
        }
예제 #2
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            // 注册连接打开的响应函数
            WebSocketManager.AddConnectionOpenHandler(OnConnectionOpen);
            // 注册连接中断的响应函数
            WebSocketManager.AddConnectionCloseHandler(OnConnectionClose);
            // 注册连接错误的响应函数
            WebSocketManager.AddConnectionErrorHandler(OnConnectionError);

            // 注册一些服务器会主动push的消息
            WebSocketManager.AddRpcHandler(RpcNameEnum.ForceOfflinePush, OnForceOfflinePushCallback);
            WebSocketManager.AddRpcHandler(RpcNameEnum.MatchResultPush, OnMatchResultPushCallback);
            WebSocketManager.AddRpcHandler(RpcNameEnum.BattleEventMsgListPush, OnBattleEventMsgListPushCallback);

            // 与服务器建立连接
            WebSocketManager.InitWebSocket();
            if (WebSocketManager.IsConnected() == false)
            {
                string tips = string.Format("无法连接服务器,请确保服务器地址为{0},且处于开启状态", AppValues.SERVER_URL);
                MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // 强制弹出登录界面
            loginForm = new LoginForm();
            loginForm.ShowDialog();
        }
예제 #3
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (WebSocketManager.IsConnected() == false)
            {
                WebSocketManager.InitWebSocket();
                if (WebSocketManager.IsConnected() == false)
                {
                    string tips = string.Format("无法连接服务器,请确保服务器地址为{0},且处于开启状态", AppValues.SERVER_URL);
                    MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            string inputUsername = txtUsername.Text.Trim();
            string inputPassword = txtPassword.Text.Trim();

            if (string.IsNullOrEmpty(inputUsername))
            {
                MessageBox.Show("用户名不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(inputPassword))
            {
                MessageBox.Show("密码不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            LoginRequest.Builder builder = LoginRequest.CreateBuilder();
            builder.SetUsername(inputUsername);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string passwordMD5           = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(inputPassword))).Replace("-", "").ToUpper();

            builder.SetPasswordMD5(passwordMD5);
            WebSocketManager.SendMessage(RpcNameEnum.Login, builder.Build().ToByteArray(), OnLoginCallback);
        }
예제 #4
0
        private void DelegateOnGetBattleInfoCallback(Object obj)
        {
            ResponseMsg msg = obj as ResponseMsg;

            if (msg.ErrorCode == RpcErrorCodeEnum.Ok)
            {
                GetBattleInfoResponse resp       = GetBattleInfoResponse.ParseFrom(msg.ProtoData);
                BattleInfo            battleInfo = resp.BattleInfo;
                AppValues.CurrentTurnInfo      = battleInfo.CurrentTurnInfo;
                AppValues.CellInfos            = new List <int>(battleInfo.BattleCellInfoList);
                AppValues.BattleUserBriefInfos = new List <UserBriefInfo>(battleInfo.UserBriefInfosList);
                AppValues.LastEventNum         = battleInfo.LastEventNum;
                RefreshUiForBattleInfo();
                // 如果自己没有确认准备开始游戏,需要准备开始
                if (battleInfo.NotReadyUserIdsList != null && battleInfo.NotReadyUserIdsList.Contains(AppValues.UserInfoBuilder.UserId))
                {
                    // 发出准备好开始对战的请求
                    ReadyToStartGameRequest.Builder builder = ReadyToStartGameRequest.CreateBuilder();
                    WebSocketManager.SendMessage(RpcNameEnum.ReadyToStartGame, builder.Build().ToByteArray(), OnReadyToStartGameCallback);
                }
            }
            else
            {
                string tips = string.Format("获取战场信息失败,errorCode = {0}", msg.ErrorCode);
                MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
예제 #5
0
 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (e.CloseReason == CloseReason.UserClosing)
     {
         isFormClosing = true;
         WebSocketManager.CloseWebSocket();
     }
 }
예제 #6
0
 private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     // 用户点击关闭登录窗口,会强制将整个软件关闭
     // 注意:当登录成功,执行LoginForm的Hide方法时,仍旧会触发LoginFrom的closing和closed方法
     // 如果不加上这个判断,就会因为LoginForm的Hide,执行下面的逻辑,导致WebSocket被关闭
     if (e.CloseReason == CloseReason.UserClosing)
     {
         WebSocketManager.CloseWebSocket();
         Environment.Exit(0);
     }
 }
예제 #7
0
        private void DelegateOnForceOfflinePushCallback(Object obj)
        {
            isReceivedForceOfflinePush = true;
            ResponseMsg      msg = obj as ResponseMsg;
            ForceOfflinePush forceOfflinePush = ForceOfflinePush.ParseFrom(msg.ProtoData);

            WebSocketManager.CloseWebSocket();
            string tips = string.Format("被服务器踢下线,原因为{0}\n请重新登录", forceOfflinePush.ForceOfflineReason);

            MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (loginForm != null && loginForm.Visible == false)
            {
                loginForm.ShowDialog();
            }
        }
예제 #8
0
        private void MainForm_Shown(object sender, EventArgs e)
        {
            if (AppValues.UserInfoBuilder == null)
            {
                this.Text = "测试服务器框架工具 by 张齐 (未登录)";
            }
            else
            {
                this.Text = string.Format("测试服务器框架工具 by 张齐 (已登录用户:{0})", AppValues.UserInfoBuilder.Username);
            }

            RefreshUiForUserState();
            // 如果已经处于对战中,就要获取当前战场信息
            if (AppValues.UserInfoBuilder.UserState.ActionState == UserActionStateEnum.Playing)
            {
                GetBattleInfoRequest.Builder builder = GetBattleInfoRequest.CreateBuilder();
                WebSocketManager.SendMessage(RpcNameEnum.GetBattleInfo, builder.Build().ToByteArray(), OnGetBattleInfoCallback);
            }
        }
예제 #9
0
        private void OnCellButtonClick(object sender, EventArgs e)
        {
            Button btn            = sender as Button;
            int    underlineIndex = btn.Name.LastIndexOf('_');
            int    index          = int.Parse(btn.Name.Substring(underlineIndex + 1, btn.Name.Length - underlineIndex - 1));

            // 校验是不是自己的回合
            if (AppValues.CurrentTurnInfo.UserId != AppValues.UserInfoBuilder.UserId)
            {
                MessageBox.Show(this, "当前是对方回合", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // 校验该位置是否没有放置棋子
            if (AppValues.CellInfos[index] != 0)
            {
                MessageBox.Show(this, "该位置已放置棋子", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            PlacePiecesRequest.Builder builder = PlacePiecesRequest.CreateBuilder();
            builder.SetLastEventNum(AppValues.LastEventNum);
            builder.SetIndex(index);
            WebSocketManager.SendMessage(RpcNameEnum.PlacePieces, builder.Build().ToByteArray(), OnPlacePiecesCallback);
        }
예제 #10
0
        private void DelegateOnMatchResultPushCallback(Object obj)
        {
            ResponseMsg     msg             = obj as ResponseMsg;
            MatchResultPush matchResultPush = MatchResultPush.ParseFrom(msg.ProtoData);

            if (matchResultPush.IsSuccess == false)
            {
                MessageBox.Show(this, "匹配失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                UserState.Builder userStateBuilder = AppValues.UserInfoBuilder.UserState.ToBuilder();
                userStateBuilder.SetActionState(UserActionStateEnum.Playing);
                userStateBuilder.SetBattleType(matchResultPush.BattleType);
                userStateBuilder.SetBattleId(matchResultPush.BattleId);
                AppValues.UserInfoBuilder.SetUserState(userStateBuilder);

                RefreshUiForUserState();

                // 请求获取战场信息
                GetBattleInfoRequest.Builder builder = GetBattleInfoRequest.CreateBuilder();
                WebSocketManager.SendMessage(RpcNameEnum.GetBattleInfo, builder.Build().ToByteArray(), OnGetBattleInfoCallback);
            }
        }
예제 #11
0
 private void btnCancelMatch_Click(object sender, EventArgs e)
 {
     CancelMatchRequest.Builder builder = CancelMatchRequest.CreateBuilder();
     WebSocketManager.SendMessage(RpcNameEnum.CancelMatch, builder.Build().ToByteArray(), OnCancelMatchCallback);
 }
예제 #12
0
 private void btnMatch_Click(object sender, EventArgs e)
 {
     MatchRequest.Builder builder = MatchRequest.CreateBuilder();
     builder.SetBattleType(BattleTypeEnum.BattleTypeTwoPlayer);
     WebSocketManager.SendMessage(RpcNameEnum.Match, builder.Build().ToByteArray(), OnMatchCallback);
 }