1

I am trying to refresh the connected ConnectionId whenever a new connection is made for all the connected connections in blazor server using signalr.
But the problem is that whenever the second or later connection is made the the same ConnectionId is added twice in the list.

Any help with code will be grate. Thank you

Below is my hub

    public class ChatHub : Hub
    {
        public Task SendMessage(string ConnectionId, string message)
        {
            return Clients.Client(ConnectionId).SendAsync("ReceiveMessage", message);
        }
        public override Task OnConnectedAsync()
        {
           Clients.All.SendAsync("ReceiveUser", Context.ConnectionId);
           return base.OnConnectedAsync();
        }
    }
    public static class UserHandler
    {
        public static List<string> ConnectedUsers = new List<string>();
    }


Below is my code to refresh connected ConnectionId

    protected override async Task OnInitializedAsync()
    {
        hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
            .Build();

        hubConnection.On<string>("ReceiveMessage", broad);
        hubConnection.On<string>("ReceiveUser", RefreshUserList);

        await hubConnection.StartAsync();
        StateHasChanged();
    }
    private void RefreshUserList(string connectedUserId) // Double hit in this method as a result same connectionId is added twice in the list
    {
        UserHandler.ConnectedUsers.Add(connectedUserId);
        connectedUserList = UserHandler.ConnectedUsers;
        StateHasChanged();
    }
sudip chand
  • 225
  • 6
  • 14

0 Answers0