问题
Using SignalR for ASP.NET Core 2.0, Client side (1.0.0-alpha2-final), C# VS2017.
To connect the code is:
await hubConnection.StartAsync();
since this is an await
for a void
how can we test if the client did manage to connect ? In my tests I have a breakpoint on the server side in public override async Task OnConnectedAsync()
but that breakpoint is not always hit therefore I know that sometimes the client does not connect. Or perhaps it did connect but OnConnectedAsync()
was not triggered?
How can the client knows it succeeded in establishing the connection?
Thanks
Edit: More info. It does fire OnConnectedAsync()
when using localhost
(debugging in VS); it does not fire OnConnectedAsync()
when connecting through ngrok. Maybe this is something to do with proxy/CORS, I am not familiar with.
Edit: It appears that the Client does indeed connect because if I change the URL to a non existing one it gives a 404. So it appears that the problem is that OnConnectedAsync()
does not fire when the client connects through ngrok but does fire when connected directly to localhost.
回答1:
StartAsync
is not a void
method but returns a Task
. If the client does not connect StartAsync
throws (returns a faulted task) otherwise the async call will complete without an error (returns a completed task) after the client is successfully connected.
来源:https://stackoverflow.com/questions/47056690/how-to-check-success-of-hubconnection-startasync-in-signalr-for-asp-net-core-2-0