public static Task NotifyOfVoteProcessedUpdateAverage(VoteProcessed v) { return Task.Factory.StartNew(() => { //Connect to the signalR hub var context = GlobalHost.ConnectionManager.GetHubContext<VoteHub>(); //Update the client of their vote context.Clients.Client(v.ConnectionId).DisplayVoteResult(v.OriginalVoteValue, true, v.CurrentAverage); //Update all clients on the latest vote context.Clients.All.UpdateLastVote(v.OriginalVoteValue); }); }
//Process good votes as quickly as we can and return the current average. public Task ProcessGoodVote(Vote vote) { return Task.Factory.StartNew(() => { if (vote.VoteValue < 0) { //Add the vote to the local store of all votes _Votes.Add(vote); var avg = GetAverageResult(); VoteProcessed processed = new VoteProcessed() { VoterId = vote.VoterId, ConnectionId = vote.ConnectionId, ProcessedDateTime = DateTime.Now, OriginalVoteValue = vote.VoteValue, CurrentAverage = avg.Average }; //Publish the latest average vote value to the bus BusHost.Publish(avg, BusTopic.NewAverageResult); //publish a message saying that the message was processed BusHost.Publish(processed, BusTopic.VoteProcessed); } }); }
//Process bad votes in out own time.. public Task ProcessBadVote(Vote vote) { return Task.Factory.StartNew(() => { if (vote.VoteValue >= 0) { //Bad votes take longer to count. Thread.Sleep(3000); //One in every 4 bad votes get lost. if ((_Votes.Count +1) % 4 == 0) { //Add the vote to the local store of all votes vote.VoteLost = true; } _Votes.Add(vote); var avg = GetAverageResult(); VoteProcessed processed = new VoteProcessed() { VoterId = vote.VoterId, ConnectionId = vote.ConnectionId, ProcessedDateTime = DateTime.Now, OriginalVoteValue = vote.VoteValue, CurrentAverage = avg.Average }; //Publish the latest average vote value to the bus BusHost.Publish(avg, BusTopic.NewAverageResult); //publish a message saying BusHost.Publish(processed, BusTopic.VoteProcessed); } }); }