Beispiel #1
0
        /// <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));
        }
        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
                );
        }
Beispiel #5
0
        /// <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);
        }