コード例 #1
0
        private void ProcessMessage(string message)
        {
            try
            {
                WebMessageBase request = JsonConvert.DeserializeObject <WebMessageBase>(message);
                Debug.WriteLine("[{0}] Client received response: {1} , status: {2}", DateTime.Now.ToString("HH:mm:ss.fff"), request.Tag, request.Status);
                string outp = String.Empty;

                if (request.Status == MessageStatus.FAIL)
                {
                    _log(request.Message);
                    return;
                }

                if (request.Tag == "unsubscription")
                {
                    _messageChain.OnNext("unsubscription");
                }

                if (request.Tag == "discount")
                {
                    _messageChain.OnNext(ProcessDiscountMessage(message));
                }
            }
            catch (Exception e)
            {
                _log("Error: " + e.Message);
            }
        }
コード例 #2
0
        private async Task <string> ProcessGetCustomerRequest(GetCustomerRequest request)
        {
            try
            {
                CustomerDto customerDto = await _orderService.CustomerService.GetCustomer(request.Customer);

                string result;

                if (customerDto == null)
                {
                    WebMessageBase response = new WebMessageBase("get_customer");
                    response.Status  = MessageStatus.FAIL;
                    response.Message = "Customer with ID: " + request.Customer + " not found";
                    result           = JsonConvert.SerializeObject(response, Formatting.Indented);
                    return(result);
                }

                GetCustomerResponse customerResponse = new GetCustomerResponse("get_customer", customerDto.FromDto());
                result = JsonConvert.SerializeObject(customerResponse, Formatting.Indented);
                return(result);
            }
            catch (Exception e)
            {
                WebMessageBase response = new WebMessageBase();
                response.Status  = MessageStatus.FAIL;
                response.Message = "Could not get customer.";
                string result = JsonConvert.SerializeObject(response, Formatting.Indented);
                _log("During processing get_customer requst an error occured: " + e.Message);
                return(result);
            }
        }
コード例 #3
0
        private async Task <string> ProcessUnsubscriptionRequest(WebMessageBase request, WebSocketConnection webSocketConnection)
        {
            try
            {
                IList <IObserver <DiscountEvent> > subs = await _orderService.GetSubscribers();

                foreach (Subscription subscription in subs)
                {
                    if (subscription.Websocket.Equals(webSocketConnection))
                    {
                        subscription.Unsubscriber.Dispose();
                        break;
                    }
                }

                WebMessageBase response = new WebMessageBase("unsubscription");
                response.Status  = MessageStatus.SUCCESS;
                response.Message = "Unsubscribe request completed.";
                string result = JsonConvert.SerializeObject(response, Formatting.Indented);
                return(result);
            }
            catch (Exception e)
            {
                WebMessageBase response = new WebMessageBase();
                response.Status  = MessageStatus.FAIL;
                response.Message = "Unsubscribe request error.";
                string result = JsonConvert.SerializeObject(response, Formatting.Indented);
                _log("During processing subscription request an error occured: " + e.Message);
                return(result);
            }
        }
コード例 #4
0
        public async Task <string> UnsubscribeDiscount()
        {
            WebMessageBase request     = new WebMessageBase("unsubscription");
            string         requestJson = JsonConvert.SerializeObject(request, Formatting.Indented);

            _clientDiscountWebSocket.ActiveMessageLoop = false;
            await _clientDiscountWebSocket.SendAsync(requestJson);

            return("unsubscription");
        }
コード例 #5
0
        public async Task <string> SubscribeDiscount()
        {
            byte[] buffer = new byte[20000];
            ArraySegment <byte> segment = new ArraySegment <byte>(buffer);
            WebMessageBase      request = new WebMessageBase("subscription");
            string requestJson          = JsonConvert.SerializeObject(request, Formatting.Indented);
            await _clientDiscountWebSocket.SendAsync(requestJson);

            bool   gotCorrectResponse = false;
            string mesg = "";

            while (!gotCorrectResponse)
            {
                WebSocketReceiveResult result = _clientDiscountWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                if (result.MessageType == WebSocketMessageType.Close)
                {
                    _clientDiscountWebSocket.Disconnect();
                    throw new Exception("Disconnected. Close message");
                }
                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        _clientDiscountWebSocket.Disconnect();
                        throw new Exception("Disconnected. Buffer overloaded");
                    }
                    segment = new ArraySegment <byte>(buffer, count, buffer.Length - count);
                    result  = _clientDiscountWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                    count  += result.Count;
                }
                mesg = Encoding.UTF8.GetString(buffer, 0, count);
                WebMessageBase baseResponse = JsonConvert.DeserializeObject <WebMessageBase>(mesg);
                if (baseResponse.Tag == "subscription")
                {
                    gotCorrectResponse = true;
                    if (baseResponse.Status != MessageStatus.SUCCESS)
                    {
                        throw new Exception(baseResponse.Message);
                    }
                }
            }
            _clientDiscountWebSocket.ActiveMessageLoop = true;
            WebMessageBase response = JsonConvert.DeserializeObject <WebMessageBase>(mesg);

            if (response.Status == MessageStatus.SUCCESS)
            {
                return(response.Message);
            }
            else
            {
                return("Could not subscribe. " + response.Message);
            }
        }
コード例 #6
0
        private async Task <string> ProcessMakeOrderRequest(OrderRequestResponse request)
        {
            try
            {
                string clientID = request.Order.ClientInfo.Id;
                if (string.IsNullOrEmpty(clientID) || string.IsNullOrWhiteSpace(clientID))
                {
                    clientID = await _orderService.CustomerService.SaveCustomer(request.Order.ClientInfo.ToDto());
                }

                CustomerDto clientDto = await _orderService.CustomerService.GetCustomer(clientID);

                string result;

                if (clientDto == null)
                {
                    WebMessageBase response = new WebMessageBase("make_order");
                    response.Status  = MessageStatus.FAIL;
                    response.Message = "Client with ID: " + clientID + " not found";
                    result           = JsonConvert.SerializeObject(response, Formatting.Indented);
                    return(result);
                }

                request.Order.ClientInfo.Id = clientID;
                string orderId = await _orderService.SaveOrder(request.Order.ToDto());

                OrderDto orderDto = await _orderService.GetOrder(orderId);

                if (orderDto == null)
                {
                    WebMessageBase response = new WebMessageBase("make_order");
                    response.Status  = MessageStatus.FAIL;
                    response.Message = "Order with ID: " + request.Order.Id + " not found";
                    result           = JsonConvert.SerializeObject(response, Formatting.Indented);
                    return(result);
                }

                OrderRequestResponse orderResponse = new OrderRequestResponse("make_order", orderDto.FromDto());
                result = JsonConvert.SerializeObject(orderResponse, Formatting.Indented);
                return(result);
            }
            catch (Exception e)
            {
                WebMessageBase response = new WebMessageBase();
                response.Status  = MessageStatus.FAIL;
                response.Message = "Make order request error.";
                string result = JsonConvert.SerializeObject(response, Formatting.Indented);
                _log("During processing save_order request an error occured: " + e.Message);
                return(result);
            }
        }
コード例 #7
0
        public async Task <string> MakeOrderRequest(Order order)
        {
            byte[] buffer = new byte[20000];
            ArraySegment <byte>  segment    = new ArraySegment <byte>(buffer);
            OrderModel           orderModel = order.ToCommModel();
            OrderRequestResponse request    = new OrderRequestResponse("make_order", orderModel);
            string requestJson = JsonConvert.SerializeObject(request, Formatting.Indented);
            await _clientWebSocket.SendAsync(requestJson);

            bool   gotCorrectResponse = false;
            string message            = "";

            while (!gotCorrectResponse)
            {
                WebSocketReceiveResult result = _clientWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                if (result.MessageType == WebSocketMessageType.Close)
                {
                    _clientWebSocket.Disconnect();
                    throw new Exception("Disconnected. Close message");
                }
                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        _clientWebSocket.Disconnect();
                        throw new Exception("Disconnected. Buffer overloaded");
                    }
                    segment = new ArraySegment <byte>(buffer, count, buffer.Length - count);
                    result  = _clientWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                    count  += result.Count;
                }
                message = Encoding.UTF8.GetString(buffer, 0, count);
                WebMessageBase baseResponse = JsonConvert.DeserializeObject <WebMessageBase>(message);
                if (baseResponse.Tag == "make_order")
                {
                    gotCorrectResponse = true;
                    if (baseResponse.Status != MessageStatus.SUCCESS)
                    {
                        throw new Exception(baseResponse.Message);
                    }
                }
            }
            OrderRequestResponse response = JsonConvert.DeserializeObject <OrderRequestResponse>(message);
            string clientId = response.Order.ClientInfo.Id;
            string orderId  = response.Order.Id;

            return("make_order:" + clientId + ":" + orderId);
        }
コード例 #8
0
        public async Task <IList <Merchandise> > GetMerchandisesRequest()
        {
            byte[] buffer = new byte[20000];
            ArraySegment <byte>    segment = new ArraySegment <byte>(buffer);
            GetMerchandisesRequest request = new GetMerchandisesRequest("get_merchandises");
            string requestJson             = JsonConvert.SerializeObject(request, Formatting.Indented);
            await _clientWebSocket.SendAsync(requestJson);

            bool   gotCorrectResponse = false;
            string message            = "";

            while (!gotCorrectResponse)
            {
                WebSocketReceiveResult result = _clientWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                if (result.MessageType == WebSocketMessageType.Close)
                {
                    _clientWebSocket.Disconnect();
                    throw new Exception("Disconnected. Close message");
                }
                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        _clientWebSocket.Disconnect();
                        throw new Exception("Disconnected. Buffer overloaded");
                    }
                    segment = new ArraySegment <byte>(buffer, count, buffer.Length - count);
                    result  = _clientWebSocket.ClientWebSocket.ReceiveAsync(segment, _timeOut).Result;
                    count  += result.Count;
                }
                message = Encoding.UTF8.GetString(buffer, 0, count);
                WebMessageBase baseResponse = JsonConvert.DeserializeObject <WebMessageBase>(message);
                if (baseResponse.Tag == "get_merchandises")
                {
                    gotCorrectResponse = true;
                    if (baseResponse.Status != MessageStatus.SUCCESS)
                    {
                        throw new Exception(baseResponse.Message);
                    }
                }
            }
            GetMerchandisesResponse response = JsonConvert.DeserializeObject <GetMerchandisesResponse>(message);

            return(response.Merchandises.FromCommModel());
        }
コード例 #9
0
 private async Task <string> ProcessGetMerchandisesRequest(GetMerchandisesRequest request)
 {
     try
     {
         List <MerchandiseDto>   merchandiseDtos      = (await _orderService.MerchandiseService.GetMerchandises()).ToList();
         GetMerchandisesResponse merchandisesResponse = new GetMerchandisesResponse("get_merchandises", merchandiseDtos.FromDto());
         string result = JsonConvert.SerializeObject(merchandisesResponse, Formatting.Indented);
         return(result);
     }
     catch (Exception e)
     {
         WebMessageBase response = new WebMessageBase();
         response.Status  = MessageStatus.FAIL;
         response.Message = "Get merchandise request error.";
         string result = JsonConvert.SerializeObject(response, Formatting.Indented);
         _log("During processing get_merchandises request an error occured: " + e.Message);
         return(result);
     }
 }
コード例 #10
0
        private async Task <string> ProcessSubscriptionRequest(WebMessageBase request, WebSocketConnection webSocketConnections)
        {
            try
            {
                Subscription subscription = new Subscription(webSocketConnections, _log);
                subscription.Unsubscriber = await _orderService.Subscribe(subscription);

                WebMessageBase response = new WebMessageBase("subscription");
                response.Status  = MessageStatus.SUCCESS;
                response.Message = "Subscription request completed.";
                string result = JsonConvert.SerializeObject(response, Formatting.Indented);
                return(result);
            }
            catch (Exception e)
            {
                WebMessageBase response = new WebMessageBase();
                response.Status  = MessageStatus.FAIL;
                response.Message = "Subscribe request error.";
                string result = JsonConvert.SerializeObject(response, Formatting.Indented);
                _log("During processing subscription request an error occured: " + e.Message);
                return(result);
            }
        }
コード例 #11
0
 public static void SendWebMessage(WebMessageBase messageObject, CoreWebView2 webView)
 {
     webView.PostWebMessageAsJson(messageObject.ToJsonString());
 }
コード例 #12
0
        public async Task <string> Resolve(string message, WebSocketConnection ws)
        {
            WebMessageBase request = JsonConvert.DeserializeObject <WebMessageBase>(message);

            Console.WriteLine("[{0}] Resolving request: \"{1}\", status: {2}", DateTime.Now.ToString("HH:mm:ss.fff"), request.Tag, request.Status);

            string output = String.Empty;

            switch (request.Tag)
            {
            case "get_customer":
            {
                GetCustomerRequest customerRequest = JsonConvert.DeserializeObject <GetCustomerRequest>(message);
                output = await ProcessGetCustomerRequest(customerRequest);

                break;
            }

            case "get_merchandises":
            {
                GetMerchandisesRequest merchandiseRequest = JsonConvert.DeserializeObject <GetMerchandisesRequest>(message);
                output = await ProcessGetMerchandisesRequest(merchandiseRequest);

                break;
            }

            case "get_order":
            {
                GetOrderRequest ordeRequest = JsonConvert.DeserializeObject <GetOrderRequest>(message);
                output = await ProcessGetOrderRequest(ordeRequest);

                break;
            }

            case "make_order":
            {
                OrderRequestResponse orderRequest = JsonConvert.DeserializeObject <OrderRequestResponse>(message);
                output = await ProcessMakeOrderRequest(orderRequest);

                break;
            }

            case "subscription":
            {
                WebMessageBase subRequest = JsonConvert.DeserializeObject <WebMessageBase>(message);
                output = await ProcessSubscriptionRequest(subRequest, ws);

                break;
            }

            case "unsubscription":
            {
                WebMessageBase unSubRequest = JsonConvert.DeserializeObject <WebMessageBase>(message);
                output = await ProcessUnsubscriptionRequest(unSubRequest, ws);

                break;
            }
            }

            return(output);
        }