kerryjiang / WebSocket4Net

A popular .NET WebSocket Client
Apache License 2.0
768 stars 273 forks source link

Your demo code does not work at all? How to make this package work? #203

Open jonmdev opened 1 month ago

jonmdev commented 1 month ago

I have installed your package using NuGet 1.0.0-beta.4.

According to this this should work:

var websocket = new WebSocket4Net.WebSocket("ws://localhost:4001/websocket");

websocket.PackageHandler += (sender, package) => {
    Debug.WriteLine(package.Message);
    return ValueTask.CompletedTask;
};

try { await websocket.OpenAsync(); }
catch (Exception ex){ Debug.WriteLine("CAN'T CONNECT " + ex.Message); }

try { websocket.StartReceive(); }
catch (Exception ex){ Debug.WriteLine("CAN'T START RECEIVE " + ex.Message); }

try { await websocket.SendAsync("ping"); }
catch (Exception ex){ Debug.WriteLine("CAN'T SEND " + ex.Message); }

However:

1) Your example code neglects to mention that websocket.PackageHandler must return ValueTask like I show above. 2) None of this still works. I am getting null errors on the WebSocket base objects repeatedly.

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=SuperSocket.Client
  StackTrace:
   at SuperSocket.Client.EasyClient`1.<StartReceiveAsync>d__45.MoveNext()

This is triggered at your code:

    public new void StartReceive()
    {
        base.StartReceive(); //BASE IS NULL HERE????
    }

What is going on? What am I supposed to do to make this work?

kerryjiang commented 2 weeks ago

Will check this problem

kerryjiang commented 2 weeks ago

Ok, seems the unit tests don't cover this usage. I will add a unit test and check what's the problem.

kerryjiang commented 1 week ago

I don't see the same problem but I did see a problem a about close handshake in PackageHandler mode. I added a unit test and pushed the fix.

A new release will be available soon.

kerryjiang commented 1 week ago

The unit test: https://github.com/kerryjiang/WebSocket4Net/blob/6a91a01465b6727d591c89913c8fcf81b0407f0b/test/WebSocket4Net.Tests/MainTest.cs#L243-L254

kerryjiang commented 1 week ago

For your case, the problem might be websocket.OpenAsync() returned false.

kerryjiang commented 1 week ago

Just added a clear exception for this case.

kerryjiang commented 1 week ago

Please try 1.0. beta 5.