示例#1
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);
        }
示例#2
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);
            }
        }
示例#3
0
 private void CloseConnection(WebSocketConnection ws)
 {
     _log($"Closing connection to peer: {ws}");
     _sockets.Remove(ws);
 }
示例#4
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);
            }
        }
示例#5
0
 private void InitErrorHandler(WebSocketConnection ws)
 {
     ws.OnClose = () => CloseConnection(ws);
     ws.OnError = () => CloseConnection(ws);
 }