jchristn / WatsonWebsocket

A simple C# async websocket server and client for reliable transmission and receipt of data
MIT License
277 stars 53 forks source link

StartWithTimeoutAsync fails the second try. #101

Closed xbarra closed 2 years ago

xbarra commented 2 years ago

Bug Report

Operating system and version: Windows 10 Framework and runtime: .NET 6.0 Nuget version 3.0.1

Issue encountered:

When I use StartWithTimeoutAsync, after the first timeout, it fails with The WebSocket has already been started

Expected behavior:

The Client should try to start the connection again. The same code works as expected in .Net Framework 4.8

Steps to reproduce:

Call StartWithTimeoutAsyncuntil connected.

Sample code encapsulating the problem:

while (!Connected)
{
    try
    {
        await StartWithTimeoutAsync(30);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Exception details:

System.InvalidOperationException: 'The WebSocket has already been started.' StackTrace:

   at System.Net.WebSockets.ClientWebSocketOptions.ThrowIfReadOnly()
   at WatsonWebsocket.WatsonWsClient.SetInvalidCertificateAcceptance()
   at WatsonWebsocket.WatsonWsClient.<StartWithTimeoutAsync>d__51.MoveNext()
jchristn commented 2 years ago

Hi @xbarra sorry for the delay, just reproduced this, and looking into it now.

jchristn commented 2 years ago

Hi @xbarra I just published v3.0.2, could you give it a whirl? I also added the Test.ClientTimeout project to test this issue. Please re-open if the issue persists!

NuGet: https://www.nuget.org/packages/WatsonWebsocket/3.0.2 Commit: https://github.com/jchristn/WatsonWebsocket/commit/affc63f0d72d97c61bab9b4e9a2cd97455975849

xbarra commented 2 years ago

Hi @jchristn,

Don't

Hi @xbarra sorry for the delay, just reproduced this, and looking into it now.

Don't worry!

Hi @xbarra I just published v3.0.2, could you give it a whirl? I also added the Test.ClientTimeout project to test this issue. Please re-open if the issue persists!

NuGet: https://www.nuget.org/packages/WatsonWebsocket/3.0.2 Commit: affc63f

It is working perfectly now, thank you very much!

xbarra commented 2 years ago

By the way, as a workaround, I had set AcceptInvalidCertificates = false. Sorry for not mentioned that earlier, it could had saved you some debugging time.

And while checking why it was working in .Net Framework 4.8, I noticed that it was not entering in the if at line 571.

Do you want me to open an issue for this?

jchristn commented 2 years ago

Hi @xbarra I would love that! Thank you!