Reactjs signalR problem

I simply try to invoke a message and i get an error:

Unhandled Rejection (Error): An unexpected error occurred invoking ‘SendMessageToGroup’ on the server.

back-end C#

public class ChatHub : Hub
     {
         public async Task SendAll(string message)
         {
    
             await Clients.All.SendAsync("ReceiveAllMessage", message);
         }
    
    
         public async Task JoinGroup(string group)
         {
             await Groups.AddToGroupAsync(Context.ConnectionId, group);
         }
    
         public async Task SendMessageToGroup(string group, string message)
         {
             string time = DateTime.Now.ToString();
             await Clients.Groups(group).SendAsync("ReceiveMessage", message, time);
         }
    
         public override async Task OnConnectedAsync()
         {
             string userId = "user32";
    
             await Clients.Caller.SendAsync("UserConnected", userId);
             await base.OnConnectedAsync();
         }
    
     }

reactjs:

 const send = async () => {
    
     console.log("send to group")
    await Newconnection.invoke("SendMessageToGroup", cookies.Default, text)
    await Newconnection.invoke("SendAll","3r2r");
    
     const data = {
         "message" : text,
         "accesToken" : cookies.Default,
         "isHelper" : false
         }
       
           const res = await axios.post('https://localhost:44332/api/LiveChat/Message',data)
             if(res.status === 200)
             console.log("succesful send message")
 }

Have you tried wrapping those lines in a try/catch block to try to catch the error? It sounds like the b/e is failing and the f/e is just not handling the error/rejection.

I have already figured it out,
i have to use “then”

  await Newconnection.invoke("SendMessageToGroup", cookies.Default, text).then(() => Newconnection.invoke("SendAll","3r2r") )

instead of

 await Newconnection.invoke("SendMessageToGroup", cookies.Default, text)
    await Newconnection.invoke("SendAll","3r2r");

but thanks anyway

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.