static void SimulateSales(string country, string product) { using (GatewayWebSocketClient websocketClient = new GatewayWebSocketClient()) { websocketClient.ConnectAsync("ws://localhost:31024/SalesServiceWS/").Wait(); PostSalesModel postSalesModel = new PostSalesModel { Product = product, Country = country }; IWsSerializer serializer = SerializerFactory.CreateSerializer(); byte[] payload = serializer.SerializeAsync(postSalesModel).Result; WsRequestMessage mreq = new WsRequestMessage { Operation = "sell", Value = payload }; WsResponseMessage mresp = websocketClient.SendReceiveAsync(mreq, CancellationToken.None).Result; if (mresp.Result == WsResult.Error) { Console.WriteLine("Error: {0}", Encoding.UTF8.GetString(mresp.Value)); } } }
static void Main(string[] args) { PostProductModel postProductModel = new PostProductModel { // generating new user id for every iteration ProductId = 701, Quantity = 100 }; string url = "ws://127.0.0.1:3251/PublicGatewayWS"; IWsSerializer serializer = SerializerFactory.CreateSerializer(); byte[] payload = serializer.SerializeAsync(postProductModel).Result; WsRequestMessage mreq = new WsRequestMessage { PartitionKey = 701, Operation = "additem", Value = payload }; using (PublicGatewayWebSocketClient websocketClient = new PublicGatewayWebSocketClient()) { var b = websocketClient.ConnectAsync(url).Result; for (int i = 0; i < 3; i++) { WsResponseMessage mresp = websocketClient.SendReceiveAsync(mreq, CancellationToken.None).Result; PostProductModel result = serializer.DeserializeAsync <PostProductModel>(mresp.Value).Result; } } Console.ReadKey(); }
/// <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)); }
public async Task <byte[]> ProcessWsMessageAsync(byte[] wsrequest, CancellationToken cancellationToken) { ProtobufWsSerializer mserializer = new ProtobufWsSerializer(); WsRequestMessage mrequest = await mserializer.DeserializeAsync <WsRequestMessage>(wsrequest); switch (mrequest.Operation) { case "sell": { IWsSerializer pserializer = SerializerFactory.CreateSerializer(); PostSalesModel payload = await pserializer.DeserializeAsync <PostSalesModel>(mrequest.Value); //await this.PurchaseProduct(payload.ProductId, payload.Quantity); var id = payload.Country + "-" + payload.Product; var candidate = ActorProxy.Create <IProductActor>(new ActorId(id), "fabric:/ECommerceApplication"); await candidate.SellAsync(); } break; } WsResponseMessage mresponse = new WsResponseMessage { Result = WsResult.Success }; return(await mserializer.SerializeAsync(mresponse)); }
public static IWsSerializer GetPse() { if (pserializer != null) { return(pserializer); } else { return(pserializer = SerializerFactory.CreateSerializer()); } }
/// <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)); }
public async Task <int> DoWorkAsyncWebSocket(int iterationsMax, int threadId, int threadCount, int maxProductsPerThread, int delayMs) { int iterations = 0; int failedCounter = 0; try { Random rnd = new Random((threadId + 1) * DateTime.UtcNow.Millisecond); Stopwatch sw = Stopwatch.StartNew(); long executionTime = sw.ElapsedMilliseconds; // Partition the Product Ids per thread int productListOffsetMin = (this.ProductIdsToPurchase.Count / threadCount) * threadId; int productListOffsetMax = (this.ProductIdsToPurchase.Count / threadCount) * (threadId + 1) - 1; // Use Max Products Per Thread? if (maxProductsPerThread > 0) { maxProductsPerThread--; if (productListOffsetMin + maxProductsPerThread < productListOffsetMax) { productListOffsetMax = productListOffsetMin + maxProductsPerThread; } } //Console.WriteLine("{0} to {1} to {2}", threadId, productListOffsetMin, productListOffsetMax); int productId = 0; int immediateQuantity = 0; // Websocket client to public gateway using (PublicGatewayWebSocketClient websocketClient = new PublicGatewayWebSocketClient()) { await websocketClient.ConnectAsync(ConnectionFactory.WebSocketServerName); while (iterations < iterationsMax) { productId = this.ProductIdsToPurchase[rnd.Next(productListOffsetMin, productListOffsetMax)]; immediateQuantity = rnd.Next(1, 4); // Report status every X seconds if ((sw.ElapsedMilliseconds - executionTime) > ReportStatusMs) { Console.WriteLine(OutputReport(threadId, immediateQuantity, productId, delayMs, failedCounter, iterations, sw.ElapsedMilliseconds)); executionTime = sw.ElapsedMilliseconds; } // Serialize PostDataModel PostProductModel postProductModel = new PostProductModel { // generating new user id for every iteration ProductId = productId, Quantity = immediateQuantity }; IWsSerializer serializer = SerializerFactory.CreateSerializer(); byte[] payload = await serializer.SerializeAsync(postProductModel); // Websocket send & receive message spec WsRequestMessage mreq = new WsRequestMessage { PartitionKey = productId, Operation = WSOperations.AddItem, Value = payload }; WsResponseMessage mresp = await websocketClient.SendReceiveAsync(mreq, CancellationToken.None); if (mresp.Result == WsResult.Error) { Console.WriteLine("Error: {0}", Encoding.UTF8.GetString(mresp.Value)); } if (mresp.Result != WsResult.Success) { failedCounter++; } iterations++; if (delayMs > -1 && iterations < iterationsMax) { await Task.Delay(delayMs); } } Console.WriteLine( "Completed: " + OutputReport(threadId, immediateQuantity, productId, delayMs, failedCounter, iterations - 1, sw.ElapsedMilliseconds)); } } catch (Exception ex) { Console.WriteLine(ex); } return(iterations); }