2
votes

Currently I'm studying SignalR for ASP.NET Core web project, I use ASP.NET Core as a server for SignalR and web clients use SignalR Javascript Client. I'm making a chat app that will return some message when the client invoke function from the server. Here's my Javascript client function:

      this.hub.invoke(method, data)
      .then((res)=>{ alert(res);})
      .catch(err=> alert(err));
The server method:
    public async Task<IActionResult> joinRoom(string roomId)
    {
        Console.WriteLine(roomId);
        return new OkObjectResult("Ok");
    }

Problem:

  • Invocation success, Console on the server side wrote the roomId

  • However, the client side's Promise.then/Promise.catch didn't work.

When I close the server, there're multiple error on client side that said Invocation has been canceled due to the connection was closed.

My SignalR version is 1.0.3

Really, I can't find out what's the reason. I hope you can help me, please... I also tried with return a string on the server side but it didn't work too.

1

1 Answers

2
votes

You can't use IActionResults with SignalR, it's not an http request, it's an RPC style invocation. Just return the object you want to get back or make the return type Task if you don't need to return anything to the client.

public async Task<string> joinRoom(string roomId)
{
    Console.WriteLine(roomId);
    return "Ok";
}