mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-22 17:27:48 +00:00
remove timer service
This commit is contained in:
@@ -1,13 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using AutoMapper;
|
|
||||||
using LiveGameFeed.Controllers;
|
|
||||||
using LiveGameFeed.Core.MvcTimer;
|
|
||||||
using LiveGameFeed.Data.Abstract;
|
using LiveGameFeed.Data.Abstract;
|
||||||
using LiveGameFeed.Data.Repositories;
|
|
||||||
using LiveGameFeed.Hubs;
|
using LiveGameFeed.Hubs;
|
||||||
using LiveGameFeed.Models;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.SignalR.Infrastructure;
|
using Microsoft.AspNetCore.SignalR.Infrastructure;
|
||||||
|
|
||||||
@@ -20,72 +13,17 @@ namespace LiveGameFeed.Controllers
|
|||||||
|
|
||||||
private Object lockOb = new Object();
|
private Object lockOb = new Object();
|
||||||
public HomeController(IConnectionManager signalRConnectionManager,
|
public HomeController(IConnectionManager signalRConnectionManager,
|
||||||
ITimerService timerService,
|
|
||||||
IMatchRepository matchRepository,
|
IMatchRepository matchRepository,
|
||||||
IFeedRepository feedRepository)
|
IFeedRepository feedRepository)
|
||||||
: base(signalRConnectionManager)
|
: base(signalRConnectionManager)
|
||||||
{
|
{
|
||||||
_matchRepository = matchRepository;
|
_matchRepository = matchRepository;
|
||||||
_feedRepository = feedRepository;
|
_feedRepository = feedRepository;
|
||||||
// timerService.TimerElapsed += _feed_Generate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void _feed_Generate(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
TimerEventArgs eventsArgs = e as TimerEventArgs;
|
|
||||||
System.Diagnostics.Debug.WriteLine("hello from home ApiHubController.cs..");
|
|
||||||
lock (this.lockOb)
|
|
||||||
{
|
|
||||||
UpdateScores();
|
|
||||||
}
|
|
||||||
//await Clients.All.userConnected(DateTime.Now);
|
|
||||||
//_coolMessageHubContext.Clients.All.newCpuValue(eventsArgs.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void UpdateScores()
|
|
||||||
{
|
|
||||||
Random r = new Random();
|
|
||||||
bool updateHost = r.Next(0, 2) == 0;
|
|
||||||
|
|
||||||
IEnumerable<Match> matches = _matchRepository.GetAll();
|
|
||||||
|
|
||||||
foreach (var match in matches)
|
|
||||||
{
|
|
||||||
if (updateHost)
|
|
||||||
{
|
|
||||||
match.HostScore = match.HostScore + 2;
|
|
||||||
Feed feed = new Feed()
|
|
||||||
{
|
|
||||||
Description = "2 points for " + match.Host + "!",
|
|
||||||
CreatedAt = DateTime.Now,
|
|
||||||
MatchId = match.Id
|
|
||||||
};
|
|
||||||
|
|
||||||
match.Feeds.Add(feed);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
match.GuestScore = match.GuestScore + 2;
|
|
||||||
Feed feed = new Feed()
|
|
||||||
{
|
|
||||||
Description = "2 points for " + match.Guest + "!",
|
|
||||||
CreatedAt = DateTime.Now,
|
|
||||||
MatchId = match.Id
|
|
||||||
};
|
|
||||||
match.Feeds.Add(feed);
|
|
||||||
}
|
|
||||||
|
|
||||||
_matchRepository.Commit();
|
|
||||||
MatchViewModel _matchVM = Mapper.Map<Match, MatchViewModel>(match);
|
|
||||||
FeedViewModel _feedVM = Mapper.Map<Feed, FeedViewModel>(match.Feeds.Last());
|
|
||||||
await Clients.All.updateMatch(_matchVM);
|
|
||||||
await Clients.Group(match.Id.ToString()).addFeed(_feedVM);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace LiveGameFeed.Core.MvcTimer
|
|
||||||
{
|
|
||||||
public interface ITimerService
|
|
||||||
{
|
|
||||||
event EventHandler TimerElapsed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace LiveGameFeed.Core.MvcTimer
|
|
||||||
{
|
|
||||||
public class TimerEventArgs : EventArgs
|
|
||||||
{
|
|
||||||
public TimerEventArgs(int value)
|
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Value { get; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace LiveGameFeed.Core.MvcTimer
|
|
||||||
{
|
|
||||||
public class TimerService : ITimerService
|
|
||||||
{
|
|
||||||
private Timer _timer;
|
|
||||||
readonly Random _random = new Random();
|
|
||||||
public event EventHandler TimerElapsed;
|
|
||||||
|
|
||||||
public TimerService(IOptions<TimerServiceConfiguration> options)
|
|
||||||
{
|
|
||||||
var optionsTimerServiceConfiguration = options;
|
|
||||||
_timer = new Timer(
|
|
||||||
OnTimerElapsed,
|
|
||||||
null,
|
|
||||||
optionsTimerServiceConfiguration.Value.DueTime,
|
|
||||||
optionsTimerServiceConfiguration.Value.Period);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnTimerElapsed(object sender)
|
|
||||||
{
|
|
||||||
TimerElapsed?.Invoke(this, new TimerEventArgs(_random.Next(0, 100)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace LiveGameFeed.Core.MvcTimer
|
|
||||||
{
|
|
||||||
public class TimerServiceConfiguration
|
|
||||||
{
|
|
||||||
public int DueTime { get; set; }
|
|
||||||
|
|
||||||
public int Period { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,6 @@ using Microsoft.Extensions.Configuration;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using LiveGameFeed.Core.Mappings;
|
using LiveGameFeed.Core.Mappings;
|
||||||
using LiveGameFeed.Core.MvcTimer;
|
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using RecurrentTasks;
|
using RecurrentTasks;
|
||||||
using LiveGameFeed.Core;
|
using LiveGameFeed.Core;
|
||||||
@@ -41,10 +40,6 @@ namespace LiveGameFeed
|
|||||||
services.AddSingleton<IMatchRepository, MatchRepository>();
|
services.AddSingleton<IMatchRepository, MatchRepository>();
|
||||||
services.AddSingleton<IFeedRepository, FeedRepository>();
|
services.AddSingleton<IFeedRepository, FeedRepository>();
|
||||||
|
|
||||||
// Timer service configuration
|
|
||||||
services.AddSingleton<ITimerService, TimerService>();
|
|
||||||
services.Configure<TimerServiceConfiguration>(Configuration.GetSection("TimeService"));
|
|
||||||
|
|
||||||
// Automapper Configuration
|
// Automapper Configuration
|
||||||
AutoMapperConfiguration.Configure();
|
AutoMapperConfiguration.Configure();
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,5 @@
|
|||||||
"System": "Information",
|
"System": "Information",
|
||||||
"Microsoft": "Information"
|
"Microsoft": "Information"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"TimeService": {
|
|
||||||
"DueTime": 10000,
|
|
||||||
"Period": 10000
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user