Skip to content

Commit 38f2259

Browse files
Add project files.
1 parent 2805a4c commit 38f2259

11 files changed

+216
-0
lines changed

SignalR Call from ASP.NET Core.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34316.72
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignalR.Call", "SignalR.Call\SignalR.Call.csproj", "{568F8A20-2A28-4067-8442-2FD814A526FD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{568F8A20-2A28-4067-8442-2FD814A526FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{568F8A20-2A28-4067-8442-2FD814A526FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{568F8A20-2A28-4067-8442-2FD814A526FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{568F8A20-2A28-4067-8442-2FD814A526FD}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {DBC72AE7-5EC8-47B2-BE3B-E1694872783D}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.SignalR;
3+
4+
namespace SignalR.Call.Controllers
5+
{
6+
[ApiController]
7+
[Route("[controller]")]
8+
public class RandomizerController : ControllerBase
9+
{
10+
private readonly IHubContext<RandomizerHub, IRandomizerClient> _hub;
11+
private readonly TimerManager _timer;
12+
public RandomizerController(IHubContext<RandomizerHub, IRandomizerClient> hub, TimerManager timer)
13+
{
14+
_hub = hub;
15+
_timer = timer;
16+
}
17+
18+
[HttpGet("SendRandomNumber")]
19+
public ActionResult<int> SendRandomNumber()
20+
{
21+
var randomValue = Random.Shared.Next(1, 51) * 2;
22+
if (!_timer.IsTimerStarted)
23+
{
24+
_timer.PrepareTimer(() =>
25+
_hub.Clients.All
26+
.SendClientRandomEvenNumber(randomValue));
27+
}
28+
return Ok(randomValue);
29+
}
30+
}
31+
}

SignalR.Call/IRandomizerClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SignalR.Call
2+
{
3+
public interface IRandomizerClient
4+
{
5+
void SendClientRandomEvenNumber(int number);
6+
}
7+
}

SignalR.Call/Program.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using SignalR.Call;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
7+
builder.Services.AddControllers();
8+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
9+
builder.Services.AddEndpointsApiExplorer();
10+
builder.Services.AddSwaggerGen();
11+
//builder.Services.AddSingleton<IRandomizerClient, RandomizerHub>();
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (app.Environment.IsDevelopment())
16+
{
17+
app.UseSwagger();
18+
app.UseSwaggerUI();
19+
}
20+
21+
app.UseHttpsRedirection();
22+
23+
app.UseAuthorization();
24+
25+
app.MapControllers();
26+
27+
app.Run();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:45958",
8+
"sslPort": 44375
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5188",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"https": {
23+
"commandName": "Project",
24+
"dotnetRunMessages": true,
25+
"launchBrowser": true,
26+
"launchUrl": "swagger",
27+
"applicationUrl": "https://localhost:7174;http://localhost:5188",
28+
"environmentVariables": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
}
31+
},
32+
"IIS Express": {
33+
"commandName": "IISExpress",
34+
"launchBrowser": true,
35+
"launchUrl": "swagger",
36+
"environmentVariables": {
37+
"ASPNETCORE_ENVIRONMENT": "Development"
38+
}
39+
}
40+
}
41+
}

SignalR.Call/RandomizerHub.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace SignalR.Call
4+
{
5+
public class RandomizerHub : Hub<IRandomizerClient>
6+
{
7+
}
8+
}

SignalR.Call/SignalR.Call.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<InvariantGlobalization>true</InvariantGlobalization>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
12+
</ItemGroup>
13+
14+
</Project>

SignalR.Call/SignalR.Call.http

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@SignalR.Call_HostAddress = http://localhost:5188
2+
3+
GET {{SignalR.Call_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###

SignalR.Call/TimerManager.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace SignalR.Call
2+
{
3+
public class TimerManager
4+
{
5+
private Timer? _timer;
6+
private Action? _action;
7+
8+
public DateTime TimerStarted { get; set; }
9+
public bool IsTimerStarted { get; set; }
10+
11+
public void PrepareTimer(Action action)
12+
{
13+
_action = action;
14+
15+
_timer = new Timer(Execute, null, 1000, 4000);
16+
17+
TimerStarted = DateTime.Now;
18+
19+
IsTimerStarted = true;
20+
}
21+
22+
public void Execute(object? stateInfo)
23+
{
24+
if (_action != null)
25+
{
26+
_action();
27+
}
28+
29+
if ((DateTime.Now - TimerStarted).TotalSeconds > 60)
30+
{
31+
IsTimerStarted = false;
32+
33+
if (_timer != null)
34+
{
35+
_timer.Dispose();
36+
}
37+
}
38+
}
39+
}
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

SignalR.Call/appsettings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)