/// <summary> /// IWebSocketListener.ProcessWsMessageAsync /// </summary> public async Task <byte[]> ProcessWsMessageAsync(byte[] wsrequest, CancellationToken cancellationToken) { Logger.Debug(nameof(this.ProcessWsMessageAsync)); ProtobufWsSerializer mserializer = new ProtobufWsSerializer(); WsRequestMessage mrequest = await mserializer.DeserializeAsync <WsRequestMessage>(wsrequest); switch (mrequest.Operation) { case WSOperations.AddItem: { IWsSerializer pserializer = SerializerFactory.CreateSerializer(); PostProductModel payload = await pserializer.DeserializeAsync <PostProductModel>(mrequest.Value); await this.PurchaseProduct(payload.ProductId, payload.Quantity); } break; } WsResponseMessage mresponse = new WsResponseMessage { Result = WsResult.Success }; return(await mserializer.SerializeAsync(mresponse)); }
/// <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 static ProtobufWsSerializer GetMse() { if (mserializer != null) { return(mserializer); } else { return(mserializer = new ProtobufWsSerializer()); } }
/// <summary> /// Re-uses the open websocket connection (assumes one is already created/connected) /// </summary> public async Task <WsResponseMessage> SendReceiveAsync(WsRequestMessage msgspec, CancellationToken cancellationToken) { if (this.websocketManager == null) { throw new ApplicationException("SendReceiveAsync requires an open websocket client"); } // Serialize Msg payload IWsSerializer mserializer = new ProtobufWsSerializer(); byte[] request = await mserializer.SerializeAsync(msgspec); byte[] response = await this.websocketManager.SendReceiveAsync(request); return(await mserializer.DeserializeAsync <WsResponseMessage>(response)); }
public async Task <byte[]> ProcessWsMessageAsync( byte[] wsrequest, CancellationToken cancellationToken ) { IWsSerializer mserializer = new ProtobufWsSerializer(); WsRequestMessage mrequest = await mserializer.DeserializeAsync <WsRequestMessage>(wsrequest); ServicePartitionClient <WsCommunicationClient> serviceClient = new ServicePartitionClient <WsCommunicationClient>( this.clientFactory, ConnectionFactory.StockServiceUri, partitionKey: new ServicePartitionKey(mrequest.PartitionKey), //根据消息分发 listenerName: ServiceConst.ListenerWebsocket); return(await serviceClient.InvokeWithRetryAsync(async client => await client.SendReceiveAsync(wsrequest), cancellationToken)); }
public async Task<byte[]> ProcessWsMessageAsync( byte[] wsrequest, CancellationToken cancellationToken ) { IWsSerializer mserializer = new ProtobufWsSerializer(); WsRequestMessage mrequest = await mserializer.DeserializeAsync<WsRequestMessage>(wsrequest); ServicePartitionClient<WsCommunicationClient> serviceClient = new ServicePartitionClient<WsCommunicationClient>( this.clientFactory, ConnectionFactory.StockServiceUri, partitionKey: new ServicePartitionKey(mrequest.PartitionKey), listenerName: ServiceConst.ListenerWebsocket); return await serviceClient.InvokeWithRetryAsync( async client => await client.SendReceiveAsync(wsrequest), cancellationToken ); }
/// <summary> /// IWebSocketListener.ProcessWsMessageAsync 消息分发过来进行处理 /// </summary> public async Task <byte[]> ProcessWsMessageAsync(byte[] wsrequest, CancellationToken cancellationToken) { Logger.Debug(nameof(this.ProcessWsMessageAsync)); ProtobufWsSerializer mserializer = new ProtobufWsSerializer(); WsRequestMessage mrequest = await mserializer.DeserializeAsync <WsRequestMessage>(wsrequest); ///事件集中在这里处理 并且可以保存状态 switch (mrequest.Operation) { case WSOperations.AddItem: { IWsSerializer pserializer = SerializerFactory.CreateSerializer(); PostProductModel payload = await pserializer.DeserializeAsync <PostProductModel>(mrequest.Value); //取值 await this.PurchaseProduct(payload.ProductId, payload.Quantity); //数据处理 } break; } PostProductModel m = new PostProductModel(); m.ProductId = 100; m.Quantity = 200; var m1 = await mserializer.SerializeAsync(m); //构造返回值 WsResponseMessage mresponse = new WsResponseMessage { Result = WsResult.Success, Value = m1 }; return(await mserializer.SerializeAsync(mresponse)); }
/// <summary> /// IWebSocketListener.ProcessWsMessageAsync /// </summary> public async Task<byte[]> ProcessWsMessageAsync(byte[] wsrequest, CancellationToken cancellationToken) { Logger.Debug(nameof(this.ProcessWsMessageAsync)); ProtobufWsSerializer mserializer = new ProtobufWsSerializer(); WsRequestMessage mrequest = await mserializer.DeserializeAsync<WsRequestMessage>(wsrequest); switch (mrequest.Operation) { case WSOperations.AddItem: { IWsSerializer pserializer = SerializerFactory.CreateSerializer(); PostProductModel payload = await pserializer.DeserializeAsync<PostProductModel>(mrequest.Value); await this.PurchaseProduct(payload.ProductId, payload.Quantity); } break; } WsResponseMessage mresponse = new WsResponseMessage { Result = WsResult.Success }; return await mserializer.SerializeAsync(mresponse); }
/// <summary> /// Re-uses the open websocket connection (assumes one is already created/connected) /// </summary> public async Task<WsResponseMessage> SendReceiveAsync(WsRequestMessage msgspec, CancellationToken cancellationToken) { if (this.websocketManager == null) { throw new ApplicationException("SendReceiveAsync requires an open websocket client"); } // Serialize Msg payload IWsSerializer mserializer = new ProtobufWsSerializer(); byte[] request = await mserializer.SerializeAsync(msgspec); byte[] response = await this.websocketManager.SendReceiveAsync(request); return await mserializer.DeserializeAsync<WsResponseMessage>(response); }