コード例 #1
0
 public User autentizate(Credentials credentials)
 {
     if (webSocketToRealServer.IsAlive)
     {
         webSocketToRealServer.Send(JsonConvert.SerializeObject(credentials));
         while (didUserAutentizeWrapper == null) // while no response from server came
         {
             Thread.Sleep(50);                   // slow poll wait for response
         }
         if (didUserAutentizeWrapper.wasSuccessful)
         {
             return(didUserAutentizeWrapper.autentizedUser);
         }
         else
         {
             didUserAutentizeWrapper = null;
             return(null);
         }
     }
     else
     {
         didUserAutentizeWrapper = null;
         return(null);
     }
 }
コード例 #2
0
        public ServerProxy(ClientController clientController)
        {
            this.clientController  = clientController;
            this.productsInventory = new Dictionary <int, Product>();

            this.webSocketToRealServer = new WebSocket(REAL_SERVER_URL);
            // this.webSocketToRealServer.Log.Level = LogLevel.Debug;
            this.webSocketToRealServer.Connect();
            this.webSocketToRealServer.OnMessage += (sender, e) =>
            {
                Console.WriteLine("Server says: " + e.Data);
                UpdateProductsParamWrapper updateProductsParam = JsonConvert.DeserializeObject <UpdateProductsParamWrapper>(e.Data);
                if (updateProductsParam.hasValidValues())
                {
                    // update products message came
                    Console.WriteLine("Update products inventory message came");
                    this.productsInventory = updateProductsParam.productsInventory;
                    this.clientController.updateProductList(updateProductsParam.productsInventory);
                }
                else
                {   // information about result of either previous authentization or previous bidding must have come or action result message
                    DidUserAutentizeWrapper didUserAutentize = JsonConvert.DeserializeObject <DidUserAutentizeWrapper>(e.Data);
                    if (didUserAutentize.hasValidValues())
                    {
                        // autentization result message came
                        Console.WriteLine("Autentization result message came");
                        this.didUserAutentizeWrapper = didUserAutentize;
                    }
                    else
                    {
                        ProductAuctionResultWrapper productAuctionResultWrapper = JsonConvert.DeserializeObject <ProductAuctionResultWrapper>(e.Data);
                        if (productAuctionResultWrapper.hasValidValues())
                        {
                            // product auction result message came
                            Console.WriteLine("Product auction result message came");
                            this.clientController.productAuctionResultMessage(productAuctionResultWrapper);
                        }
                        else
                        {
                            // bidding result message came
                            Console.WriteLine("Bidding result message came");
                            WasBidPlacedWrapper wasBidPlaced = JsonConvert.DeserializeObject <WasBidPlacedWrapper>(e.Data);
                            this.wasBidPlacedWrapper = wasBidPlaced;
                        }
                    }
                }
            };
        }
コード例 #3
0
        protected override void OnMessage(MessageEventArgs e)
        {
            Console.WriteLine("Client says: " + e.Data);
            Credentials credentials = JsonConvert.DeserializeObject <Credentials>(e.Data);

            if (!credentials.userName.IsNullOrEmpty() && !credentials.userName.IsNullOrEmpty())
            {
                // authentization message came
                Console.WriteLine("Authentization message came");
                User autentizedUser = autentizate(credentials);
                bool wasAutentized  = autentizedUser != null;
                DidUserAutentizeWrapper didUserAutentizeWrapper = new DidUserAutentizeWrapper(wasAutentized, wasAutentized ? autentizedUser : new User(0, credentials));
                Console.WriteLine("Client " + credentials.userName + " has tried to autentizate");
                Console.WriteLine("Result " + (wasAutentized ? "Succeeded" : "Failed"));
                Sessions.SendTo(JsonConvert.SerializeObject(didUserAutentizeWrapper), this.ID); // notify client
            }
            else
            {
                // bid product message must have come
                Console.WriteLine("Bidding message came");
                BidProductParamsWrapper bidProductParams = JsonConvert.DeserializeObject <BidProductParamsWrapper>(e.Data);
                if (bidProductParams.hasValidValues())
                {
                    bool wasSuccessful = bidProduct(
                        bidProductParams.productID, bidProductParams.bidValue, bidProductParams.bidder
                        );
                    Sessions.SendTo(JsonConvert.SerializeObject(new WasBidPlacedWrapper(wasSuccessful)), this.ID); // notify client
                    Thread.Sleep(50);
                    this.serverController.notifyAllClientsAboutProductChange();
                }
                else
                {
                    throw new Exception("Unknown message came from the client");
                }
            }
        }