-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a32e62
commit a6d0e4d
Showing
13 changed files
with
184 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |