mirror of
https://github.com/PacktPublishing/Web-Development-with-Blazor-Second-Edition.git
synced 2025-12-06 05:32:03 +01:00
34 lines
1 KiB
C#
34 lines
1 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
using Data.Models;
|
|
using Components.Interfaces;
|
|
|
|
namespace BlazorWebAssembly.Client.Services;
|
|
|
|
public class BlazorWebAssemblyBlogNotificationService : IBlogNotificationService, IAsyncDisposable
|
|
{
|
|
public BlazorWebAssemblyBlogNotificationService(NavigationManager navigationManager)
|
|
{
|
|
_hubConnection = new HubConnectionBuilder()
|
|
.WithUrl(navigationManager.ToAbsoluteUri("/BlogNotificationHub"))
|
|
.Build();
|
|
_hubConnection.On<BlogPost>("BlogPostChanged", (post) =>
|
|
{
|
|
BlogPostChanged?.Invoke(post);
|
|
});
|
|
_hubConnection.StartAsync();
|
|
}
|
|
|
|
private readonly HubConnection _hubConnection;
|
|
public event Action<BlogPost>? BlogPostChanged;
|
|
|
|
public async Task SendNotification(BlogPost post)
|
|
{
|
|
await _hubConnection.SendAsync("SendNotification", post);
|
|
}
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await _hubConnection.DisposeAsync();
|
|
}
|
|
}
|