示例#1
0
        private async void SendAuctionResponse()
        {
            GetAuctionResponse resp = new GetAuctionResponse
            {
                Method = "GetAuctionResponse",
                AdId   = 0
            };

            if (CurrentAuction != null)
            {
                resp.AdId          = CurrentAuction.AdId;
                resp.CountDown     = CurrentAuction.CountDown;
                resp.BidList       = CurrentAuction.BidList;
                resp.CurrentPrice  = CurrentAuction.CurrentPrice;
                resp.InitialPrice  = CurrentAuction.InitialPrice;
                resp.TotalDuration = CurrentAuction.TotalDuration;
                resp.TimeRemaining = Convert.ToInt32(CurrentAuction.StartTime.Subtract(DateTime.UtcNow).TotalSeconds);
            }
            string respStr = JsonSerializer.Serialize <GetAuctionResponse>(resp);
            await Clients.All.SendAsync("ReceiveMessage", "Admin", respStr);
        }
示例#2
0
        public async Task SendMessage(string user, string message)
        {
            string         input = message;
            AuctionMessage msg;

            try
            {
                msg = JsonSerializer.Deserialize <AuctionMessage>(input);
            }catch (Exception e)
            {
                msg = null;
            }
            if (msg != null)
            {
                switch (msg.Method)
                {
                case "GetAuctionRequest":
                    GetAuctionResponse response = new GetAuctionResponse
                    {
                        Method = "GetAuctionResponse",
                        AdId   = 0,
                    };
                    if (CurrentAuction != null)
                    {
                        response.AdId          = CurrentAuction.AdId;
                        response.CountDown     = CurrentAuction.CountDown;
                        response.BidList       = CurrentAuction.BidList;
                        response.CurrentPrice  = CurrentAuction.CurrentPrice;
                        response.InitialPrice  = CurrentAuction.InitialPrice;
                        response.TotalDuration = CurrentAuction.TotalDuration;
                        response.TimeRemaining = Convert.ToInt32(CurrentAuction.StartTime.Subtract(DateTime.UtcNow).TotalSeconds);
                    }
                    string resStr = JsonSerializer.Serialize <GetAuctionResponse>(response);
                    await Clients.Client((string)ConIds[user]).SendAsync("ReceiveMessage", user, resStr);

                    break;

                case "StartAuctionRequest":
                    StartAuctionRequest request;
                    try
                    {
                        request = JsonSerializer.Deserialize <StartAuctionRequest>(input);
                    }
                    catch (Exception e)
                    {
                        request = null;
                    }
                    if (request != null)
                    {
                        if (CurrentAuction == null)
                        {
                            CurrentAuction               = new Auction();
                            CurrentAuction.BidList       = new List <Bid>();
                            CurrentAuction.AdId          = request.AdId;
                            CurrentAuction.CountDown     = request.CountDown;
                            CurrentAuction.InitialPrice  = request.InitialPrice;
                            CurrentAuction.CurrentPrice  = request.InitialPrice;
                            CurrentAuction.TotalDuration = request.TotalDuration;
                            CurrentAuction.TimeRemaining = request.TotalDuration;
                            CurrentAuction.StartTime     = DateTime.UtcNow.AddSeconds(Convert.ToDouble(request.TotalDuration));
                            AuctionTimer = new System.Threading.Timer(AuctionTimerExpired, null,
                                                                      request.TotalDuration * 1000, 0);
                            //BidTimer = new System.Threading.Timer(BidTimerExpired, null,
                            //    BidInterval, 0);
                        }
                    }
                    SendAuctionResponse();
                    break;

                case "BidRequest":
                    BidRequest bidRequest;
                    try
                    {
                        bidRequest = JsonSerializer.Deserialize <BidRequest>(input);
                    }
                    catch (Exception e)
                    {
                        bidRequest = null;
                    }
                    if (bidRequest != null)
                    {
                        if (CurrentAuction.CurrentPrice < bidRequest.Price)
                        {
                            CurrentAuction.CurrentPrice = bidRequest.Price;
                            Bid item = new Bid();
                            item.user  = user;
                            item.price = bidRequest.Price;
                            int timeRemaining = Convert.ToInt32(CurrentAuction.StartTime.Subtract(DateTime.UtcNow).TotalSeconds);
                            item.bidTime = CurrentAuction.TotalDuration - timeRemaining;
                            CurrentAuction.BidList.Add(item);
                            //BidTimer.Change(BidInterval, 0);
                        }
                    }
                    SendAuctionResponse();
                    break;

                default:
                    break;
                }
            }
            else
            {
                await Clients.All.SendAsync("ReceiveMessage", user, message);
            }

/*
 *          if (msg != null && msg.Action == "GetAuction")
 *          {
 *              //GetAuction getAucObj = (GetAuction)msg;
 *              GetAuction getAucObj = JsonSerializer.Deserialize<GetAuction>(message);
 *              await Clients.All.SendAsync("ReceiveMessage", user, getAucObj.CurrentAuction);
 *          }
 *          else
 *          {
 *              await Clients.All.SendAsync("ReceiveMessage", user, message);
 *          }
 */
        }