Ejemplo n.º 1
0
        private void DelegateOnRegistCallback(Object obj)
        {
            ResponseMsg msg = obj as ResponseMsg;

            if (msg.ErrorCode == RpcErrorCodeEnum.Ok)
            {
                MessageBox.Show(this, "注册成功,请您登录", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string tips = string.Format("登录失败,errorCode = {0}", msg.ErrorCode);
                MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
0
        private void DelegateOnLoginCallback(Object obj)
        {
            ResponseMsg msg = obj as ResponseMsg;

            if (msg.ErrorCode == RpcErrorCodeEnum.Ok)
            {
                LoginResponse resp     = LoginResponse.ParseFrom(msg.ProtoData);
                UserInfo      userInfo = resp.UserInfo;
                AppValues.UserInfoBuilder = userInfo.ToBuilder();
                this.Hide();
            }
            else
            {
                string tips = string.Format("登录失败,errorCode = {0}", msg.ErrorCode);
                MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 4
0
        private void DelegateOnPlacePiecesCallback(Object obj)
        {
            ResponseMsg msg = obj as ResponseMsg;

            if (msg.ErrorCode == RpcErrorCodeEnum.Ok)
            {
                PlacePiecesResponse resp = PlacePiecesResponse.ParseFrom(msg.ProtoData);
                foreach (EventMsg eventMsg in resp.EventList.MsgListList)
                {
                    doOneEvent(eventMsg);
                }
            }
            else
            {
                string tips = string.Format("落子操作错误,errorCode = {0}", msg.ErrorCode);
                MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 5
0
        private void DelegateOnCancelMatchCallback(Object obj)
        {
            ResponseMsg msg = obj as ResponseMsg;

            if (msg.ErrorCode == RpcErrorCodeEnum.Ok)
            {
                UserState.Builder userStateBuilder = AppValues.UserInfoBuilder.UserState.ToBuilder();
                userStateBuilder.SetActionState(UserActionStateEnum.ActionNone);
                AppValues.UserInfoBuilder.SetUserState(userStateBuilder);

                RefreshUiForUserState();
            }
            else
            {
                string tips = string.Format("取消匹配失败,errorCode = {0}", msg.ErrorCode);
                MessageBox.Show(this, tips, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
0
        private void OnLoginCallback(ResponseMsg msg)
        {
            OperateUiDelegate delegateFunc = new OperateUiDelegate(DelegateOnLoginCallback);

            this.Invoke(delegateFunc, msg);
        }
Ejemplo n.º 8
0
        private void OnReadyToStartGameCallback(ResponseMsg msg)
        {
            OperateUiDelegate delegateFunc = new OperateUiDelegate(DelegateOnReadyToStartGameCallback);

            this.Invoke(delegateFunc, msg);
        }
Ejemplo n.º 9
0
        private void OnBattleEventMsgListPushCallback(ResponseMsg msg)
        {
            OperateUiDelegate delegateFunc = new OperateUiDelegate(DelegateOnBattleEventMsgListPushCallback);

            this.Invoke(delegateFunc, msg);
        }
Ejemplo n.º 10
0
        private void OnMatchResultPushCallback(ResponseMsg msg)
        {
            OperateUiDelegate delegateFunc = new OperateUiDelegate(DelegateOnMatchResultPushCallback);

            this.Invoke(delegateFunc, msg);
        }
Ejemplo n.º 11
0
        private void OnForceOfflinePushCallback(ResponseMsg msg)
        {
            OperateUiDelegate delegateFunc = new OperateUiDelegate(DelegateOnForceOfflinePushCallback);

            this.Invoke(delegateFunc, msg);
        }
Ejemplo n.º 12
0
        public static void InitWebSocket()
        {
            if (ws != null)
            {
                ws.Close();
            }

            ws            = new WebSocket(AppValues.SERVER_URL);
            ws.OnMessage += (sender, e) =>
            {
                if (e.IsBinary)
                {
                    ResponseMsg msg     = new ResponseMsg();
                    int         rpcNum  = IntEncodingUtil.ByteArrayToIntByBigEndian(e.RawData, 4);
                    RpcNameEnum rpcName = (RpcNameEnum)rpcNum;
                    msg.RpcName = rpcName;
                    int errorCode = IntEncodingUtil.ByteArrayToIntByBigEndian(e.RawData, 8);
                    RpcErrorCodeEnum rpcErrorCode = (RpcErrorCodeEnum)errorCode;
                    msg.ErrorCode = rpcErrorCode;
                    if (rpcErrorCode == RpcErrorCodeEnum.Ok)
                    {
                        int protoDataLength = e.RawData.Length - 12;
                        msg.ProtoData = new byte[protoDataLength];
                        Buffer.BlockCopy(e.RawData, 12, msg.ProtoData, 0, protoDataLength);
                    }
                    if (rpcHandlerDict.ContainsKey(rpcName))
                    {
                        rpcHandlerDict[rpcName](msg);
                    }
                }
                else
                {
                    Debug.WriteLine("收到非二进制数据");
                }
            };
            ws.OnOpen += (sender, e) =>
            {
                Debug.WriteLine("连接成功");
                // 开启定时心跳计时器,避免长时间空闲被服务器踢下线
                if (heartbeatTimer != null)
                {
                    heartbeatTimer.Close();
                    heartbeatTimer = null;
                }
                if (AppValues.HEARTBEAT_INTERVAL_MSEC > 0)
                {
                    heartbeatTimer           = new Timer(AppValues.HEARTBEAT_INTERVAL_MSEC);
                    heartbeatTimer.Elapsed  += new ElapsedEventHandler(OnHeartbeatTimer);
                    heartbeatTimer.AutoReset = true;
                    heartbeatTimer.Enabled   = true;
                }

                foreach (var hander in connectionOpenHandlerSet)
                {
                    hander(e);
                }
            };
            ws.OnError += (sender, e) =>
            {
                Debug.WriteLine("发生错误:" + e.Message);
                foreach (var hander in connectionErrorHandlerSet)
                {
                    hander(e);
                }
            };
            ws.OnClose += (sender, e) =>
            {
                Debug.WriteLine("连接关闭");
                if (heartbeatTimer != null)
                {
                    heartbeatTimer.Close();
                    heartbeatTimer = null;
                }

                foreach (var hander in connectionCloseHandlerSet)
                {
                    hander(e);
                }
            };

            ws.Connect();
        }