Skip to content

Commit

Permalink
Introduce SignalR
Browse files Browse the repository at this point in the history
  • Loading branch information
C0nquistadore committed Aug 11, 2022
1 parent 2a32e62 commit a6d0e4d
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 13 deletions.
13 changes: 6 additions & 7 deletions Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<GlobalPackageReference Include="Nerdbank.GitVersioning" Version="3.4.255" />

<PackageReference Update="coverlet.collector" Version="$(CoverletVersion)" >
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTestSdkVersion)" />
<PackageReference Update="MSTest.TestAdapter" Version="$(MSTestAdapterVersion)" />
<PackageReference Update="MSTest.TestFramework" Version="$(MSTestFrameworkVersion)" />
<PackageReference Update="coverlet.collector" Version="$(CoverletVersion)" PrivateAssets="all" IncludeAssets="runtime;build;native;contentfiles;analyzers;buildtransitive" />
<PackageReference Update="Microsoft.AspNetCore.SignalR.Client" Version="$(MicrosoftAspNetCoreSignalRClientVersion)" />
<PackageReference Update="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsHostingVersion)" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTestSdkVersion)" />
<PackageReference Update="MSTest.TestAdapter" Version="$(MSTestAdapterVersion)" />
<PackageReference Update="MSTest.TestFramework" Version="$(MSTestFrameworkVersion)" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions PhoneBox.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PhoneBox.Server", "src\Phon
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PhoneBox.Server.Test", "tests\PhoneBox.Server.Test\PhoneBox.Server.Test.csproj", "{63FC2FE6-35A1-4759-8C43-DAB86346B4BE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PhoneBox.Client", "src\PhoneBox.Client\PhoneBox.Client.csproj", "{B509D9D4-8C41-469D-9FB8-E8F90C747D8B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -60,6 +62,10 @@ Global
{63FC2FE6-35A1-4759-8C43-DAB86346B4BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63FC2FE6-35A1-4759-8C43-DAB86346B4BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63FC2FE6-35A1-4759-8C43-DAB86346B4BE}.Release|Any CPU.Build.0 = Release|Any CPU
{B509D9D4-8C41-469D-9FB8-E8F90C747D8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B509D9D4-8C41-469D-9FB8-E8F90C747D8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B509D9D4-8C41-469D-9FB8-E8F90C747D8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B509D9D4-8C41-469D-9FB8-E8F90C747D8B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -68,6 +74,7 @@ Global
{D471D863-FB0C-44E3-904F-A3B8CC0B0256} = {62C58971-8516-42E1-8D2A-70A0CE779FFE}
{5AD40A13-FDF9-4A8E-823C-4AED252BAB4F} = {118B0BCE-C81F-4DA0-814C-2F8AC4867710}
{63FC2FE6-35A1-4759-8C43-DAB86346B4BE} = {B1D5F4EA-842C-494E-9B2F-1FF271591E29}
{B509D9D4-8C41-469D-9FB8-E8F90C747D8B} = {118B0BCE-C81F-4DA0-814C-2F8AC4867710}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F25BA702-70BE-4E04-8583-C71369C6D3D0}
Expand Down
2 changes: 2 additions & 0 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<PropertyGroup>

<!-- Runtime -->
<MicrosoftAspNetCoreSignalRClientVersion>6.0.8</MicrosoftAspNetCoreSignalRClientVersion>
<MicrosoftExtensionsHostingVersion>6.0.1</MicrosoftExtensionsHostingVersion>

<!-- Testing -->
<CoverletVersion>3.1.2</CoverletVersion>
Expand Down
9 changes: 9 additions & 0 deletions shared/ITelephonyHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace PhoneBox
{
public interface ITelephonyHub
{
Task SendMessage(string message);
}
}
10 changes: 10 additions & 0 deletions src/PhoneBox.Client/HubConnectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.SignalR.Client
{
internal static class HubConnectionExtensions
{
public static IDisposable On<T1>(this HubConnection hubConnection, Func<T1, Task> handler) => hubConnection.On(handler.Method.Name, handler);
}
}
17 changes: 17 additions & 0 deletions src/PhoneBox.Client/PhoneBox.Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\shared\ITelephonyHub.cs" Link="ITelephonyHub.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions src/PhoneBox.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace PhoneBox.Client
{
internal static class Program
{
private static async Task Main(string[] args)
{
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<TelephonyHubListener>();
})
.Build();

await host.RunAsync().ConfigureAwait(false);
}
}
}
63 changes: 63 additions & 0 deletions src/PhoneBox.Client/TelephonyHubListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Hosting;

namespace PhoneBox.Client
{
internal sealed class TelephonyHubListener : IHostedService, ITelephonyHub
{
#region IHostedService Members
public async Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Starting listener");

HubConnection connection = new HubConnectionBuilder().WithUrl("https://localhost:63440/TelephonyHub")
.WithAutomaticReconnect()
.Build();
connection.Closed += OnHubConnectionClosed;
connection.On<string>(this.SendMessage);

await connection.StartAsync(cancellationToken).ConfigureAwait(false);

Console.WriteLine("Listener started");
}

public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Stopping listener");
Console.WriteLine("Listener stopped");
return Task.CompletedTask;
}

private static Task OnHubConnectionClosed(Exception? exception)
{
Console.WriteLine("Hub connection closed");
if (exception != null)
{
try
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(exception);
}
finally
{
Console.ResetColor();

}
}

return Task.CompletedTask;
}
#endregion

#region ITelephonyHub Members
public Task SendMessage(string message)
{
Console.WriteLine($"Received message: {message}");
return Task.CompletedTask;
}
#endregion
}
}
4 changes: 4 additions & 0 deletions src/PhoneBox.Server/PhoneBox.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\shared\ITelephonyHub.cs" Link="ITelephonyHub.cs" />
</ItemGroup>

</Project>
14 changes: 10 additions & 4 deletions src/PhoneBox.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace PhoneBox.Server
{
internal static class Program
{
private static async Task Main(string[] args)
{
WebApplication app = WebApplication.CreateBuilder(args)
.Build();
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

app.MapGet("/hi", () => "Hello!");
IServiceCollection services = builder.Services;
services.AddSignalR();
services.AddHostedService<TelephonyHubPublisher>();

WebApplication app = builder.Build();

app.MapHub<TelephonyHub>("/TelephonyHub");

await app.RunAsync().ConfigureAwait(false);
}
}
}
}
3 changes: 1 addition & 2 deletions src/PhoneBox.Server/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"profiles": {
"PhoneBox.Server": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "hi",
"dotnetRunMessages": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
Expand Down
8 changes: 8 additions & 0 deletions src/PhoneBox.Server/TelephonyHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.SignalR;

namespace PhoneBox.Server
{
internal sealed class TelephonyHub : Hub<ITelephonyHub>
{
}
}
26 changes: 26 additions & 0 deletions src/PhoneBox.Server/TelephonyHubPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;

namespace PhoneBox.Server
{
internal sealed class TelephonyHubPublisher : BackgroundService
{
private readonly IHubContext<TelephonyHub, ITelephonyHub> _hub;

public TelephonyHubPublisher(IHubContext<TelephonyHub, ITelephonyHub> hub)
{
this._hub = hub;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await this._hub.Clients.All.SendMessage("Hey there").ConfigureAwait(false);
await Task.Delay(1000, stoppingToken).ConfigureAwait(false);
}
}
}
}

0 comments on commit a6d0e4d

Please sign in to comment.