sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.72k stars 1.66k forks source link

Problem when integrating websocket-sharp into my library #84

Open Meai1 opened 10 years ago

Meai1 commented 10 years ago

I suddenly out of the blue get exceptions after a few seconds for every connection. I just need to do a connect() and then wait and it will happen. The same exact thing will work just fine inside the Example3 that I'm trying. I don't get it. I tried to debug it and it seems that my connection is getting caught in a "Sweep", but those should only occur every 60 seconds and surely they shouldn't cause exceptions like this right? How do I get to the root cause of this?

what am i supposed to do

Does this mean anything? A "manual reset event" apparently happened? untitled

sta commented 10 years ago

Your websocket-sharp isn't latest? If not, could you use the latest?

And then, could you try the following option?

httpsv.KeepClean = false;

Will you get the same exceptions?

And, which client do you use? From a web browser?

Meai1 commented 10 years ago

Okay, I think I narrowed the problem down quite a bit:

A. Something about the Ping/Pong mechanism is working weirdly. Why does it sometimes wait significantly longer than the 1(?) second default wait time for pong responses until the error comes? Somehow the sweep and the ping/ping is interlocked? If this can be made consistent to expectations it may not even be a bug. I will show below how to reproduce this easily. B. I get fatal exceptions on the serverside when I don't call ws.Close() on the client. This is definitely a bug in my opinion, that's the entire reason for sweeping: it should be a fairly normal occurence that a connection just vanishes or aborts or errors out. At most I would expect a trace log. Also easily reproducable with the same code as below.

First: Yes, it also happens with the latest version, I just updated from git.

How to reproduce: Start Example3 Start the following code in another solution. (it's important that it's another solution in another Visual Studio instance!)

The code I'm using to connect:

        var ws = new WebSocket("ws://localhost:4649/Echo");
        ws.OnMessage += (sender, e) =>
        {
            Console.WriteLine("i got notified about: " + e.Data);
        };

        ws.Connect(); // A. set breakpoint here and wait! In Visual Studio you should break on all thrown exceptions, this will make it more obvious apart from the logging
        ws.Send("hello");
       // B. just let the client program exit / finish running here and you will see a fatal exception on the server

Oh and: Yes, for 1. the "httpsv.KeepClean = false;" did work, but it's not really a solution. Somehow we need to fix the dangling connections!

sta commented 10 years ago

I think it's due to "abnormal closing" (and also occurs when doesn't receive close frame in a time).

So, for now, could you try with the latest and the following option?

httpsv.WaitTime = TimeSpan.FromSeconds (most_suitable_time_for_your_apps);

I continue to consider better workaround.

Meai1 commented 10 years ago

Yes, changing the WaitTime is a workaround just like completely disabling the sweep was: It works. But eventually some client will abort his connection and I will get a fatal exception. Sounds to me like something that would seriously and irrevokably destroy operation of the server. Is that really the case? I think changing the exception to a trace log saying "session with id: X timed out" would be more appropiate.

sta commented 10 years ago

Sounds to me like something that would seriously and irrevokably destroy operation of the server. Is that really the case?

I think there's no need to worry about it. ツ