@code { private ElementReference imgRef; private string imageUrl;
protected override async Task OnInitializedAsync()
{
while (true)
{
imageUrl = await GetImageUrlAsync();
await InvokeAsync(() => StateHasChanged());
await Task.Delay(100); // Change image every 100ms
}
}
}
@code { private string imageUrl;
protected override async Task OnInitializedAsync()
{
while (true)
{
imageUrl = await GetImageUrlAsync();
await Task.Delay(100); // Change image every 100ms
await InvokeAsync(() => StateHasChanged());
}
}
protected override bool ShouldRender()
{
return true;
}
}
// Client
// JavaScript var connection = new signalR.HubConnectionBuilder().withUrl("/imageHub").build(); connection.on("ReceiveImage", function (imageUrl) { document.getElementById("image").src = imageUrl; }); await connection.start();
// Server public class ImageHub : Hub { private readonly Timer _timer; private string _imageUrl;
public ImageHub()
{
_timer = new Timer(OnTimerElapsed, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(100));
}
private async void OnTimerElapsed(object state)
{
_imageUrl = await GetImageUrlAsync();
await Clients.All.SendAsync("ReceiveImage", _imageUrl);
}
}