简体   繁体   中英

How To Refresh Data For Same Logged-in User In Multiple Window In Blazor Signalr

Here I am trying to refresh data on all the same page open at multiple windows in the same browser or a different browser for the same logged-in user using logged-in user UserIdentifier in blzor server app.

But the problem is that the data gets refreshed in only one window doesn't get refreshed in all the windows for the same logged-in user.

My thinking: if I provide user identifier to SignalR hub await Clients.Users(useridentifier).SendAsync("testrec", useridentifier); it will refresh data for that user in all its logged-in window for that useridentifier.



Below are my code

    public async Task deleteSentRequest()
    {
        mainService.DeleteRequest(friend.FriendRequestSenderName, friend.FriendRequestReceiverName);
        await OnDeletionRequest.InvokeAsync(chattingUserEmail);
    }

  <DisplayFriendRequest OnDeletionRequest="RequestDeletionEventHandler" friend="item" chattingUserEmail="@ChattingUserEmail" ></DisplayFriendRequest>


Below is my EventHandler

    public async Task RequestDeletionEventHandler(string chattingUserEmail)
    {
        var getUserIdentifier = mainService.GetUserIdentifier(chattingUserEmail);
        await hubConnection.SendAsync("RefreshUser", getUserIdentifier);
    }

Below is my hub
    hubConnection.On<string>("Receive", Reloaduser);
    public void Reloaduser(string userIdentifier)
    {
        var chattingUserEmail = mainService.GetUserEmailByUserIdentifier(userIdentifier);
        friendRequestList = mainService.GetAllFriendRequest(chattingUserEmail);
        FriendRequestCount = friendRequestList.Count();
    }

 hubConnection.On<string>("Receive", Reloaduser); public void Reloaduser(string userIdentifier) { var chattingUserEmail = mainService.GetUserEmailByUserIdentifier(userIdentifier); friendRequestList = mainService.GetAllFriendRequest(chattingUserEmail); FriendRequestCount = friendRequestList.Count(); }

You are almost there, The problem is you are not looping through each user connection. just the first that matches. You'll need to update the RefreshUser code to something like this (I am making a huge assumption here on how you wrote the app):

[Authorize]
public class Chat:Hub
{
    public async Task RefreshUser(string useridentifier)
    {
        foreach(var user in Clients.Users.Where(x => x.Email == useridentifier))
        {
            await user.SendAsync("Receive", useridentifier);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM