@page "/SignalRIndex/" @using Microsoft.AspNetCore.SignalR.Client @inject NavigationManager NavigationManager @implements IAsyncDisposable

@code { private HubConnection hubConnection; private List messages = new List(); private string userInput; private string messageInput; protected override async Task OnInitializedAsync() { hubConnection = new HubConnectionBuilder() .WithUrl(NavigationManager.ToAbsoluteUri("/chathub")) .Build(); hubConnection.On("ReceiveMessage", (user, message) => { var encodedMsg = $"{user}: {message}"; messages.Add(encodedMsg); hubConnection.SendAsync("SendNotification", userInput); StateHasChanged(); }); hubConnection.On("ShowNotification", (user, message) => { var encodedMsg = $"{user}: {message}"; messages.Add(encodedMsg); StateHasChanged(); }); await hubConnection.StartAsync(); } async Task Send() => await hubConnection.SendAsync("SendMessage", userInput, messageInput); public bool IsConnected => hubConnection.State == HubConnectionState.Connected; public async ValueTask DisposeAsync() { if (hubConnection is not null) { await hubConnection.DisposeAsync(); } } }