add SignalR
This commit is contained in:
		
							parent
							
								
									5f9787902f
								
							
						
					
					
						commit
						c5c2ac29cf
					
				
							
								
								
									
										9
									
								
								EnotaryoPH/EnotaryoPH.Web/Common/Hubs/NotificationHub.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								EnotaryoPH/EnotaryoPH.Web/Common/Hubs/NotificationHub.cs
									
									
									
									
									
										Normal 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); | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -9,10 +9,16 @@ | ||||
| 
 | ||||
| 	<ItemGroup> | ||||
| 		<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="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="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> | ||||
| @ -21,6 +27,7 @@ | ||||
| 
 | ||||
| 	<ItemGroup> | ||||
| 	  <Folder Include="wwwroot\lib\jquery.unobtrusive-ajax\" /> | ||||
| 	  <Folder Include="wwwroot\lib\signalr\" /> | ||||
| 	</ItemGroup> | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -94,6 +94,35 @@ | ||||
|     <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.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) | ||||
| </body> | ||||
| 
 | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| using System.Security.Principal; | ||||
| using Coravel; | ||||
| using EnotaryoPH.Data; | ||||
| using EnotaryoPH.Web.Common.Hubs; | ||||
| using EnotaryoPH.Web.Common.Jobs; | ||||
| using EnotaryoPH.Web.Common.Models; | ||||
| using Exadel.Compreface.Clients.CompreFaceClient; | ||||
| @ -29,6 +30,8 @@ namespace EnotaryoPH.Web | ||||
| #if DEBUG | ||||
|             razorBuilder.AddRazorRuntimeCompilation(); | ||||
| #endif | ||||
|             builder.Services.AddSignalR(); | ||||
|             builder.Services.AddSingleton<NotificationService>(); | ||||
| 
 | ||||
|             var config = builder.Configuration; | ||||
| 
 | ||||
| @ -90,6 +93,8 @@ namespace EnotaryoPH.Web | ||||
|             app.MapRazorPages() | ||||
|                .WithStaticAssets(); | ||||
| 
 | ||||
|             app.MapHub<NotificationHub>("/notificationHub"); | ||||
| 
 | ||||
|             app.Run(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
							
								
								
									
										2
									
								
								EnotaryoPH/EnotaryoPH.Web/wwwroot/lib/signalr/signalr.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								EnotaryoPH/EnotaryoPH.Web/wwwroot/lib/signalr/signalr.min.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user