mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-22 17:27:48 +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.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.SignalR.Infrastructure;
|
||||
|
||||
namespace ChatLe.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
public class HomeController : ApiHubController<Broadcaster>
|
||||
{
|
||||
static ILogger _logger;
|
||||
public HomeController(ILoggerFactory factory)
|
||||
public HomeController(IConnectionManager signalRConnectionManager,
|
||||
ITimerService timerService)
|
||||
: base(signalRConnectionManager)
|
||||
{
|
||||
if (_logger == null)
|
||||
_logger = factory.CreateLogger("Unhandled Error");
|
||||
timerService.TimerElapsed += _feed_Generate;
|
||||
}
|
||||
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
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;
|
||||
|
||||
namespace LiveGameFeed.Mappings
|
||||
namespace LiveGameFeed.Core.Mappings
|
||||
{
|
||||
public class AutoMapperConfiguration
|
||||
{
|
||||
@@ -1,7 +1,7 @@
|
||||
using AutoMapper;
|
||||
using LiveGameFeed.Models;
|
||||
|
||||
namespace LiveGameFeed.Mappings
|
||||
namespace LiveGameFeed.Core.Mappings
|
||||
{
|
||||
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
|
||||
{
|
||||
Host = "Panathinaikos",
|
||||
Guest = "Olimpiakos",
|
||||
Host = "Team 1",
|
||||
Guest = "Team 2",
|
||||
HostScore = 3,
|
||||
GuestScore = 1,
|
||||
MatchDate = DateTime.Now,
|
||||
@@ -44,8 +44,8 @@ namespace LiveGameFeed.Data
|
||||
};
|
||||
Match match_02 = new Match
|
||||
{
|
||||
Host = "Real Madrit FC",
|
||||
Guest = "Barchelona",
|
||||
Host = "Team 3",
|
||||
Guest = "Team 4",
|
||||
HostScore = 5,
|
||||
GuestScore = 3,
|
||||
MatchDate = DateTime.Now,
|
||||
|
||||
@@ -11,7 +11,8 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using LiveGameFeed.Mappings;
|
||||
using LiveGameFeed.Core.Mappings;
|
||||
using LiveGameFeed.Core.MvcTimer;
|
||||
|
||||
namespace LiveGameFeed
|
||||
{
|
||||
@@ -38,6 +39,10 @@ namespace LiveGameFeed
|
||||
services.AddScoped<IMatchRepository, MatchRepository>();
|
||||
services.AddScoped<IFeedRepository, FeedRepository>();
|
||||
|
||||
// Timer service configuration
|
||||
services.AddSingleton<ITimerService, TimerService>();
|
||||
services.Configure<TimerServiceConfiguration>(Configuration.GetSection("TimeService"));
|
||||
|
||||
// Automapper Configuration
|
||||
AutoMapperConfiguration.Configure();
|
||||
|
||||
|
||||
@@ -6,5 +6,9 @@
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
},
|
||||
"TimeService": {
|
||||
"DueTime": 3000,
|
||||
"Period": 1500
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user