/// <summary> /// 传入客户端数据 /// </summary> /// <param name="session"></param> /// <param name="value"></param> public async void OnDataReceivedAsync(WebSocketSession session, byte[] value) { //收到数据处理 //对数据 value 进行 protobuf解包处理 验证sign 对于sign不正确的 直接返回 保留正确的包继续逻辑 WsRequestMessage mrequest = InitHelpers.GetMse().DeserializeAsync <WsRequestMessage>(value).Result; //if (!mrequest.SignKey.Equals(CheckSign(sign))) //{ //TODO 验证处理 // return null; //} byte[] wsresponse = null; try { // dispatch to App provided function with requested // wsresponse = handler.ProcessWsMessageAsync(value, session.SessionID, cancellationToken).Result; wsresponse = _msgQueue.ProcessWsMessageAsync(value, session.SessionID, cancellationToken).Result; } catch (Exception ex) { // catch any error in the appAction and notify the client wsresponse = new ProtobufWsSerializer().SerializeAsync( new WsResponseMessage { MsgId = 0, Result = (int)WsResult.Error, Value = Encoding.UTF8.GetBytes(ex.Message) }).Result; } session.Send(wsresponse, 0, wsresponse.Length); //TODO: 收到二进制数据处理 }
public async Task <byte[]> ProcessWsMessageAsync(byte[] wsrequest, string session, CancellationToken cancellationToken) { Logger.Debug(nameof(this.ProcessWsMessageAsync)); WsRequestMessage mrequest = await InitHelpers.GetMse().DeserializeAsync <WsRequestMessage>(wsrequest); WsResponseMessage mresponse = new WsResponseMessage(); mresponse.Result = (int)WsResult.Success; if (dicRequest.ContainsKey((WSRequestMsgID)mrequest.MsgId)) { Account account = null; UserRole role = null; Bag bag = null; var info = dicRequest[(WSRequestMsgID)mrequest.MsgId]; if (info.ndAccount) { account = await GetAccount(session); // account = await DataHelper.GetAccountBySessionAsync(this.StateManager, session); if (account == null) { mresponse.Result = (int)WsResult.NotAccount; return(await InitHelpers.GetMse().SerializeAsync(mresponse)); } } if (info.ndRole) { role = await RoleDataHelper.Instance.GetRoleBySidAsync(session); //role = await DataHelper.GetOnlineRoleBySessionAsync(this.StateManager, session); if (role == null) { mresponse.Result = (int)WsResult.NotRole; return(await InitHelpers.GetMse().SerializeAsync(mresponse)); } bag = await BagDataHelper.Instance.GetBagByRoleId(role.Id); if (bag == null) { bag = new Bag(); } } if (mresponse.Result == (int)WsResult.Success) { SetRole(session, account, role, mrequest.Data, bag); var ret = await info.handle(); mresponse.Result = (int)ret.Result; mresponse.MsgId = (int)info.send; mresponse.Value = await InitHelpers.GetMse().SerializeAsync(ret); } } else { mresponse.Result = (int)WsResult.NoneActionFunc; } return(await InitHelpers.GetMse().SerializeAsync(mresponse)); }
public static async Task <byte[]> Make(WSResponseMsgID msgId, byte[] data) { WsResponseMessage result = new WsResponseMessage(); result.MsgId = (int)msgId; result.Result = 0; result.Value = data; return(await InitHelpers.GetMse().SerializeAsync(result)); }