aspnet / SignalR

[Archived] Incredibly simple real-time web for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
2.38k stars 446 forks source link

Cannot send data if the connection is not in the 'Connected' State. #3294

Closed akousmata closed 6 years ago

akousmata commented 6 years ago

Doing a really simple call from the client to the server following this article's example. When I originally set it up, I had the following code:

const transactionConnection =
    new signalR.HubConnectionBuilder()
           .withUrl("https://localhost:44313//TransactionHub")
           .configureLogging(signalR.LogLevel.Trace)
           .build();        

transactionConnection.on('ReceiveMessage', function (evt, transactionNotification) {
    if (!transactionNotification)
        return;

    // Do stuff
});

transactionConnection.start().catch(err => console.error(err.toString()));
transactionConnection.invoke('JoinGroup', 'ClientAccountTransaction').catch(err => console.error(err.toString()));

If I launch the page this way, I get the error in the title (client debug is attached). signalrdebug.txt

It seemed like based on the order of the log messages, that the connection needed time to get established, so I added a button to the page with an on click event that calls:

transactionConnection.invoke('JoinGroup', 'ClientAccountTransaction').catch(err => console.error(err.toString()));

And that worked. Unfortunately, there's no way I can present that to the client, this is all supposed to work under the hood. Is my assumption correct about the timing of the calls and if so what do you recommend we do to work around this?

BrennanConroy commented 6 years ago

This is fixed in the 1.1.0 preview builds, you can try them out, or if that isn't possible add a small delay between start and invoke.

akousmata commented 6 years ago

When do you anticipate releasing 1.1.0?

BrennanConroy commented 6 years ago

Soon is all I can say, https://github.com/aspnet/AspNetCore/wiki/Roadmap#schedule

akousmata commented 6 years ago

so the fix is in the server side code?

BrennanConroy commented 6 years ago

No, it's on the client side. It wasn't waiting for the handshake response before returning from start.

akousmata commented 6 years ago

I see, but you're going to time the release with .Net Core 2.2.

davidfowl commented 6 years ago

Looking at the above code though there's a bug where you're invoking after calling start without waiting on start to complete.

transactionConnection.start().catch(err => console.error(err.toString()));
transactionConnection.invoke('JoinGroup', 'ClientAccountTransaction').catch(err => console.error(err.toString()));

This is incorrect, it should be:

transactionConnection.start().then(() => {
    transactionConnection.invoke('JoinGroup', 'ClientAccountTransaction').catch(err => console.error(err.toString()));
});
akousmata commented 6 years ago

@BrennanConroy David's answer worked on my end.

BrennanConroy commented 6 years ago

Yeah, I missed that you weren't waiting for start to finish. Although you might see the issue occasionally still because of the bug I was referring to.