add SignalR

This commit is contained in:
jojo aquino 2025-02-25 20:32:59 +00:00
parent 5f9787902f
commit c5c2ac29cf
6 changed files with 68 additions and 2 deletions

View File

@ -0,0 +1,9 @@
using Microsoft.AspNetCore.SignalR;
namespace EnotaryoPH.Web.Common.Hubs
{
public class NotificationHub : Hub
{
public async Task NotifyUserAsync(string user_UID, string message) => await Clients.All.SendAsync("ReceiveNotaryNotification", user_UID, message);
}
}

View File

@ -0,0 +1,14 @@
using EnotaryoPH.Web.Common.Hubs;
using Microsoft.AspNetCore.SignalR;
namespace EnotaryoPH.Web.Common.Services
{
public class NotificationService
{
private readonly IHubContext<NotificationHub> _hubContext;
public NotificationService(IHubContext<NotificationHub> hubContext) => _hubContext = hubContext;
public async Task NotifyUserAsync(string user_UID, string message) => await _hubContext.Clients.All.SendAsync("ReceiveUserNotification", user_UID, message);
}
}

View File

@ -9,10 +9,16 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CompreFace.NET.Sdk" Version="1.0.2" /> <PackageReference Include="CompreFace.NET.Sdk" Version="1.0.2" />
<PackageReference Include="Coravel" Version="6.0.1" /> <PackageReference Include="Coravel" Version="6.0.2" />
<PackageReference Include="Coravel.Mailer" Version="7.1.0" /> <PackageReference Include="Coravel.Mailer" Version="7.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="9.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="9.0.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.1" />
<PackageReference Include="MimeKit" Version="4.9.0" /> <PackageReference Include="MimeKit" Version="4.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
<PackageReference Include="System.Text.Json" Version="9.0.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -21,6 +27,7 @@
<ItemGroup> <ItemGroup>
<Folder Include="wwwroot\lib\jquery.unobtrusive-ajax\" /> <Folder Include="wwwroot\lib\jquery.unobtrusive-ajax\" />
<Folder Include="wwwroot\lib\signalr\" />
</ItemGroup> </ItemGroup>

View File

@ -94,6 +94,35 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/baguettebox.js/1.11.1/baguetteBox.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/baguettebox.js/1.11.1/baguetteBox.min.js"></script>
<script src="/js/Lightbox-Gallery-baguetteBox.min.js"></script> <script src="/js/Lightbox-Gallery-baguetteBox.min.js"></script>
<script src="/js/Lightbox-Gallery.js"></script> <script src="/js/Lightbox-Gallery.js"></script>
@if(User.Identity.IsAuthenticated) {
<script src="/lib/signalr/signalr.min.js"></script>
<script>
var receiveUserNotificationCallback;
function tryParseJson(data) {
try {
return JSON.parse(data);
} catch (ex) {
console.error('data is not JSON');
}
return {};
}
const connection = new signalR.HubConnectionBuilder()
.withUrl("/notificationHub")
.build();
connection.on("ReceiveUserNotification", (user_UID, message) => {
receiveUserNotificationCallback?.(message);
});
connection.start().then(() => {
console.log("SignalR Connected.");
}).catch(err => console.error(err));
</script>
}
@await RenderSectionAsync("Scripts", required: false) @await RenderSectionAsync("Scripts", required: false)
</body> </body>

View File

@ -1,6 +1,7 @@
using System.Security.Principal; using System.Security.Principal;
using Coravel; using Coravel;
using EnotaryoPH.Data; using EnotaryoPH.Data;
using EnotaryoPH.Web.Common.Hubs;
using EnotaryoPH.Web.Common.Jobs; using EnotaryoPH.Web.Common.Jobs;
using EnotaryoPH.Web.Common.Models; using EnotaryoPH.Web.Common.Models;
using Exadel.Compreface.Clients.CompreFaceClient; using Exadel.Compreface.Clients.CompreFaceClient;
@ -29,6 +30,8 @@ namespace EnotaryoPH.Web
#if DEBUG #if DEBUG
razorBuilder.AddRazorRuntimeCompilation(); razorBuilder.AddRazorRuntimeCompilation();
#endif #endif
builder.Services.AddSignalR();
builder.Services.AddSingleton<NotificationService>();
var config = builder.Configuration; var config = builder.Configuration;
@ -90,6 +93,8 @@ namespace EnotaryoPH.Web
app.MapRazorPages() app.MapRazorPages()
.WithStaticAssets(); .WithStaticAssets();
app.MapHub<NotificationHub>("/notificationHub");
app.Run(); app.Run();
} }
} }

File diff suppressed because one or more lines are too long