@page "/{room}" @using Microsoft.AspNetCore.SignalR.Client @using spanreed.Shared @inject NavigationManager NavigationManager @implements IDisposable @inject HttpClient Http

@code { private HubConnection hubConnection; private List messages = new List(); private string userInput; private string messageInput; [Parameter] public string room {get; set;} protected override async Task OnParametersSetAsync() { if (hubConnection != null) { try { await hubConnection.StopAsync(); } finally { await hubConnection.DisposeAsync(); } } messages.Clear(); messageInput = ""; hubConnection = new HubConnectionBuilder().WithUrl(NavigationManager.ToAbsoluteUri("/sendmessage")).Build(); hubConnection.On("Received", (author, content, timestamp) => { messages.Add(new ChatData{Author = author, Content = content, Timestamp = timestamp}); StateHasChanged(); }); var old_messages = await Http.GetFromJsonAsync($"Chat/{room}"); if (old_messages != null) { foreach (var message in old_messages) { messages.Add(message); } } await hubConnection.StartAsync(); await hubConnection.SendAsync("Connect", room); } Task Send() => hubConnection.SendAsync("sendMessage", userInput, messageInput, room, DateTime.Now); public bool IsConnected => hubConnection.State == HubConnectionState.Connected; public void Dispose() { _ = hubConnection.DisposeAsync(); } }