mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-23 01:37:59 +00:00
added timer service to home controller
This commit is contained in:
@@ -1,21 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using LiveGameFeed.Controllers;
|
||||||
|
using LiveGameFeed.Core.MvcTimer;
|
||||||
|
using LiveGameFeed.Hubs;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.AspNetCore.SignalR.Infrastructure;
|
||||||
|
|
||||||
namespace ChatLe.Controllers
|
namespace ChatLe.Controllers
|
||||||
{
|
{
|
||||||
public class HomeController : Controller
|
public class HomeController : ApiHubController<Broadcaster>
|
||||||
{
|
{
|
||||||
static ILogger _logger;
|
public HomeController(IConnectionManager signalRConnectionManager,
|
||||||
public HomeController(ILoggerFactory factory)
|
ITimerService timerService)
|
||||||
|
: base(signalRConnectionManager)
|
||||||
{
|
{
|
||||||
if (_logger == null)
|
timerService.TimerElapsed += _feed_Generate;
|
||||||
_logger = factory.CreateLogger("Unhandled Error");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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..");
|
||||||
|
await Clients.All.userConnected(DateTime.Now);
|
||||||
|
//_coolMessageHubContext.Clients.All.newCpuValue(eventsArgs.Value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
|
||||||
namespace LiveGameFeed.Mappings
|
namespace LiveGameFeed.Core.Mappings
|
||||||
{
|
{
|
||||||
public class AutoMapperConfiguration
|
public class AutoMapperConfiguration
|
||||||
{
|
{
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using LiveGameFeed.Models;
|
using LiveGameFeed.Models;
|
||||||
|
|
||||||
namespace LiveGameFeed.Mappings
|
namespace LiveGameFeed.Core.Mappings
|
||||||
{
|
{
|
||||||
public class DomainToViewModelMappingProfile : Profile
|
public class DomainToViewModelMappingProfile : Profile
|
||||||
{
|
{
|
||||||
9
Core/Timer/ITimerService.cs
Normal file
9
Core/Timer/ITimerService.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace LiveGameFeed.Core.MvcTimer
|
||||||
|
{
|
||||||
|
public interface ITimerService
|
||||||
|
{
|
||||||
|
event EventHandler TimerElapsed;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Core/Timer/TimerEventArgs.cs
Normal file
14
Core/Timer/TimerEventArgs.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace LiveGameFeed.Core.MvcTimer
|
||||||
|
{
|
||||||
|
public class TimerEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public TimerEventArgs(int value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Value { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
28
Core/Timer/TimerService.cs
Normal file
28
Core/Timer/TimerService.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Core/Timer/TimerServiceConfiguration.cs
Normal file
9
Core/Timer/TimerServiceConfiguration.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace LiveGameFeed.Core.MvcTimer
|
||||||
|
{
|
||||||
|
public class TimerServiceConfiguration
|
||||||
|
{
|
||||||
|
public int DueTime { get; set; }
|
||||||
|
|
||||||
|
public int Period { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,8 +22,8 @@ namespace LiveGameFeed.Data
|
|||||||
{
|
{
|
||||||
Match match_01 = new Match
|
Match match_01 = new Match
|
||||||
{
|
{
|
||||||
Host = "Panathinaikos",
|
Host = "Team 1",
|
||||||
Guest = "Olimpiakos",
|
Guest = "Team 2",
|
||||||
HostScore = 3,
|
HostScore = 3,
|
||||||
GuestScore = 1,
|
GuestScore = 1,
|
||||||
MatchDate = DateTime.Now,
|
MatchDate = DateTime.Now,
|
||||||
@@ -44,8 +44,8 @@ namespace LiveGameFeed.Data
|
|||||||
};
|
};
|
||||||
Match match_02 = new Match
|
Match match_02 = new Match
|
||||||
{
|
{
|
||||||
Host = "Real Madrit FC",
|
Host = "Team 3",
|
||||||
Guest = "Barchelona",
|
Guest = "Team 4",
|
||||||
HostScore = 5,
|
HostScore = 5,
|
||||||
GuestScore = 3,
|
GuestScore = 3,
|
||||||
MatchDate = DateTime.Now,
|
MatchDate = DateTime.Now,
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using LiveGameFeed.Mappings;
|
using LiveGameFeed.Core.Mappings;
|
||||||
|
using LiveGameFeed.Core.MvcTimer;
|
||||||
|
|
||||||
namespace LiveGameFeed
|
namespace LiveGameFeed
|
||||||
{
|
{
|
||||||
@@ -38,6 +39,10 @@ namespace LiveGameFeed
|
|||||||
services.AddScoped<IMatchRepository, MatchRepository>();
|
services.AddScoped<IMatchRepository, MatchRepository>();
|
||||||
services.AddScoped<IFeedRepository, FeedRepository>();
|
services.AddScoped<IFeedRepository, FeedRepository>();
|
||||||
|
|
||||||
|
// Timer service configuration
|
||||||
|
services.AddSingleton<ITimerService, TimerService>();
|
||||||
|
services.Configure<TimerServiceConfiguration>(Configuration.GetSection("TimeService"));
|
||||||
|
|
||||||
// Automapper Configuration
|
// Automapper Configuration
|
||||||
AutoMapperConfiguration.Configure();
|
AutoMapperConfiguration.Configure();
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,9 @@
|
|||||||
"System": "Information",
|
"System": "Information",
|
||||||
"Microsoft": "Information"
|
"Microsoft": "Information"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"TimeService": {
|
||||||
|
"DueTime": 3000,
|
||||||
|
"Period": 1500
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user