Hi @issam boughanmi ,
The issue might relate that the Rcv_ConsPatReady function is not called and might be the issue relate the if condition (if (c != null)
).
Try to set a break point the in Rcv_ConsPatReady function and check them?
Besides, I suggest you could refer the Blazor Official document: Use ASP.NET Core SignalR with Blazor
Generally, in the Hub class, the SendMessage method can be called by a connected client to send a message to all clients.:
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
And in the client side, using the HubConnection.On()
method to register a handler that will be invoked when the hub method with the specified method name is invoked.
protected override async Task OnInitializedAsync()
{
hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
.Build();
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{
var encodedMsg = $"{user}: {message}";
messages.Add(encodedMsg);
StateHasChanged();
});
await hubConnection.StartAsync();
}
async Task Send() =>
await hubConnection.SendAsync("SendMessage", userInput, messageInput);
From the above code, we can see that, in the button click event (Send), it will call the hub's SendMessage
method first, then in the SendMessage
method, using the Clients.All.SendAsync
method to call client-side ReceiveMessage
method and send message to all clients.
So, in your scenario, we should ensure the Rcv_ConsPatReady function is called first, then check the if condition.
And based on your description, I have also created a sample, you can refer it:
and the ChatHub.cs
public class ChatHub: Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
public async Task SendNotification(string user)
{
var message = "send you one notification";
await Clients.All.SendAsync("ShowNotification", user, message);
}
}
In this sample, in the chathub.cs, we defined two methods: SendMessage and SendNotification, in the Client side, in the ReceiveMessage
function, we can call the SendNotification
method to send notification (since there have two clients, it will show the notification twice). The result as below:
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Dillion