From 462a0233d7369bc9b33a6c581d919b7b8d3e3e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C3=BAc=20Ph=E1=BA=A1m=20H=E1=BB=93ng?= Date: Sat, 1 Jun 2024 21:48:44 +0700 Subject: [PATCH] feat: add signalR --- BuildingBlocks.sln | 7 +++++++ .../BuildingBlock.Infrastructure.SignalR.csproj | 12 ++++++++++++ .../BuildingBlock.Infrastructure.SignalR/Hub.cs | 11 +++++++++++ .../Extensions/RealTimeExtension.cs | 13 +++++++++++++ .../Middlewares/RealTimeMiddleware.cs | 15 +++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 Infrastructure/BuildingBlock.Infrastructure.SignalR/BuildingBlock.Infrastructure.SignalR.csproj create mode 100644 Infrastructure/BuildingBlock.Infrastructure.SignalR/Hub.cs create mode 100644 Presentation/BuildingBlock.Presentation.API/Extensions/RealTimeExtension.cs create mode 100644 Presentation/BuildingBlock.Presentation.API/Middlewares/RealTimeMiddleware.cs diff --git a/BuildingBlocks.sln b/BuildingBlocks.sln index 79da048..cf5de85 100644 --- a/BuildingBlocks.sln +++ b/BuildingBlocks.sln @@ -24,6 +24,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{CFE5A713-D EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{9BFF2EA3-E081-4A2B-8297-A028DDD73E4A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuildingBlock.Infrastructure.SignalR", "Infrastructure\BuildingBlock.Infrastructure.SignalR\BuildingBlock.Infrastructure.SignalR.csproj", "{6DED74C6-3D90-409E-8D97-91CD5522ECA2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -66,6 +68,10 @@ Global {C9891FE2-E5C6-4773-BCA0-919FDCC132D2}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9891FE2-E5C6-4773-BCA0-919FDCC132D2}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9891FE2-E5C6-4773-BCA0-919FDCC132D2}.Release|Any CPU.Build.0 = Release|Any CPU + {6DED74C6-3D90-409E-8D97-91CD5522ECA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6DED74C6-3D90-409E-8D97-91CD5522ECA2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6DED74C6-3D90-409E-8D97-91CD5522ECA2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6DED74C6-3D90-409E-8D97-91CD5522ECA2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {0C2BE5AF-98DD-4E1D-A385-98E6B2D8A3A2} = {37ABC0F6-39B9-4057-955A-BA2CFDB64811} @@ -77,5 +83,6 @@ Global {4E1A0628-01F4-4876-B943-AC9FAFB19238} = {9BFF2EA3-E081-4A2B-8297-A028DDD73E4A} {2667E7B6-8451-4842-9CC5-0663C8CAE5F4} = {9BFF2EA3-E081-4A2B-8297-A028DDD73E4A} {C9891FE2-E5C6-4773-BCA0-919FDCC132D2} = {9BFF2EA3-E081-4A2B-8297-A028DDD73E4A} + {6DED74C6-3D90-409E-8D97-91CD5522ECA2} = {9BFF2EA3-E081-4A2B-8297-A028DDD73E4A} EndGlobalSection EndGlobal diff --git a/Infrastructure/BuildingBlock.Infrastructure.SignalR/BuildingBlock.Infrastructure.SignalR.csproj b/Infrastructure/BuildingBlock.Infrastructure.SignalR/BuildingBlock.Infrastructure.SignalR.csproj new file mode 100644 index 0000000..d039745 --- /dev/null +++ b/Infrastructure/BuildingBlock.Infrastructure.SignalR/BuildingBlock.Infrastructure.SignalR.csproj @@ -0,0 +1,12 @@ + + + + net8.0 + enable + enable + + + + + + diff --git a/Infrastructure/BuildingBlock.Infrastructure.SignalR/Hub.cs b/Infrastructure/BuildingBlock.Infrastructure.SignalR/Hub.cs new file mode 100644 index 0000000..50a32e9 --- /dev/null +++ b/Infrastructure/BuildingBlock.Infrastructure.SignalR/Hub.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.SignalR; + +namespace BuildingBlock.Infrastructure.SignalR; + +public class Hub : Microsoft.AspNetCore.SignalR.Hub +{ + public override async Task OnConnectedAsync() + { + await Clients.All.SendAsync("ReceiveMessage", $"{Context.ConnectionId} joined the chat"); + } +} \ No newline at end of file diff --git a/Presentation/BuildingBlock.Presentation.API/Extensions/RealTimeExtension.cs b/Presentation/BuildingBlock.Presentation.API/Extensions/RealTimeExtension.cs new file mode 100644 index 0000000..c38c0f3 --- /dev/null +++ b/Presentation/BuildingBlock.Presentation.API/Extensions/RealTimeExtension.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace BuildingBlock.Presentation.API.Extensions; + +public static class RealTimeExtension +{ + public static IServiceCollection AddRealTime(this IServiceCollection services) + { + services.AddSignalR(); + + return services; + } +} \ No newline at end of file diff --git a/Presentation/BuildingBlock.Presentation.API/Middlewares/RealTimeMiddleware.cs b/Presentation/BuildingBlock.Presentation.API/Middlewares/RealTimeMiddleware.cs new file mode 100644 index 0000000..5d08b9c --- /dev/null +++ b/Presentation/BuildingBlock.Presentation.API/Middlewares/RealTimeMiddleware.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.SignalR; + +namespace BuildingBlock.Presentation.API.Middlewares; + +public static class RealTimeMiddleware +{ + public static IEndpointRouteBuilder UseRealTime(this IEndpointRouteBuilder app) + { + app.MapHub("hub"); + + return app; + } +} \ No newline at end of file