Esempio n. 1
0
        private async Task HandleWebSocketRequest(HttpContext context, RelatedToBothService <Purchase, PurchaseDTO> purchaseService)
        {
            NiceWebSocket webSocket  = new NiceWebSocket(context);
            int           purchaseId = await ReadPurchaseId(webSocket, purchaseService);

            bool   payed       = false;
            bool   timeout     = false;
            Thread waitPayment = new Thread(() =>
            {
                payed   = WaitPayment(purchaseId);
                timeout = true;
            });

            Monitor.Add(purchaseId, waitPayment);
            ThreadVsTask(waitPayment, () => webSocket.Read());
            await webSocket.Close(
                payed? "Payed" :
                timeout? "Timeout" :
                "Interrupted"
                );
        }
Esempio n. 2
0
        private async Task <int> ReadPurchaseId(NiceWebSocket webSocket, RelatedToBothService <Purchase, PurchaseDTO> purchaseService)
        {
            string purchaseIdStr = await webSocket.Read();

            int purchaseId;

            try
            {
                purchaseId = Int32.Parse(purchaseIdStr);
                Purchase purchase = purchaseService.FindEntity(purchaseId);
                if (purchase.PayingTime != null)
                {
                    throw new AppException("Already payed");
                }
            }
            catch (Exception e)
            {
                object errorObject = e is AppException ? ((AppException)e).Content : "Not a number";
                await webSocket.Error(JsonSerializer.Serialize(errorObject));

                throw new AppException(errorObject);
            }
            return(purchaseId);
        }