コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task Run()
        {
            while (!CancellationTokenSource.IsCancellationRequested)
            {
                var buff   = new ArraySegment <byte>(new byte[1024 * 1024 * 1]);//1M缓冲区
                var result = await _WS.ReceiveAsync(buff, CancellationTokenSource.Token);

                if (result.MessageType != WebSocketMessageType.Text)
                {
                    continue;
                }
                var msg     = Encoding.UTF8.GetString(buff.Array).Trim();
                var msgbase = JsonConvert.DeserializeObject <Messages.MessageBase>(msg);
                switch (msgbase.MessageType)
                {
                    #region 查询方块
                case Enums.MessageType.queryBlocks:
                {
                    var param  = JsonConvert.DeserializeObject <Messages.QueryBlocks>(msgbase.Params);
                    var blocks = await _MapAccessQueue.AddTaskAsync2(queryBlocks, param);

                    var messageresult = new Messages.MessageBase()
                    {
                        MessageType = Enums.MessageType.queryBlocks,
                        Params      = JsonConvert.SerializeObject(blocks)
                    };
                    await _WS.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageresult))), WebSocketMessageType.Text, true, CancellationTokenSource.Token);

                    break;
                }

                    #endregion
                    #region 登录
                case Enums.MessageType.Login:
                {
                    var messageresult = await Task.Factory.StartNew <Messages.MessageBaseResponse>(async() =>
                        {
                            var param = JsonConvert.DeserializeObject <Messages.Login>(msgbase.Params);
                            if (_user != null)
                            {
                                return new Messages.MessageBaseResponse()
                                {
                                    Code = -1, Msg = "您已经登录过了!"
                                }
                            }
                            ;
                            //首先在redis里查询
                            _user = await _Redis.HashGetAsync <Modules.Users>("Users", param.Token);
                            if (_user == null)
                            {
                                return new Messages.MessageBaseResponse()
                                {
                                    Code = -1, Msg = "无效的Token!"
                                }
                            }
                            ;


                            _user = await _MapAccessQueue.AddTaskAsync2(Login, param);
                            return(new Messages.MessageBaseResponse()
                            {
                                Code = 0, Msg = "登录成功!"
                            });
                        });

                    await _WS.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageresult))), WebSocketMessageType.Text, true, CancellationTokenSource.Token);

                    break;
                }

                    #endregion
                default:    //默认
                    break;
                }
            } //End While
        }