sta / websocket-sharp

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

Client Connection closes after ~60 seconds #43

Open perara opened 10 years ago

perara commented 10 years ago

Good Morning STA!

I'm getting the following exception after approx 60 seconds of my client running:

09.05.2014 15:08:28|Fatal|WebSocket.acceptException|WebSocketSharp.WebSocketException: The header part of a frame cannot be read from the data source.
    at WebSocketSharp.WsFrame.<>c__DisplayClasse.<ParseAsync>b__d(Byte[] header)
    at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)

09.05.2014 15:08:28|Fatal|WebSocket.acceptException|System.ObjectDisposedException: Cannot access a disposed object.
    Object name: 'System.Net.Sockets.NetworkStream'.
        at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
        at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)

Service.cs:56   [ERROR]         Client: An exception has occurred while receiving a message.

--------------------------------------------------------------------------------------

09.05.2014 15:08:42|Fatal|WebSocket.acceptException|System.ObjectDisposedException: Cannot access a disposed object.
    Object name: 'System.Net.Sockets.NetworkStream'.
    at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
    at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytes

    Async>b__5(IAsyncResult ar)
    09.05.2014 15:08:42|Fatal|WebSocket.acceptException|WebSocketSharp.WebSocketException: The header part of a frame cannot be read from the data source.
    at WebSocketSharp.WsFrame.<>c__DisplayClasse.<ParseAsync>b__d(Byte[] header)
    at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)

GameEngine.cs:372       [GAME]          per used Per
Service.cs:61   [GAME]          Client: 127.0.0.1:1542 disconnected. Reason:
Client: 127.0.0.1:1542 disconnected. Reason: 09.05.2014 15:18:23|Fatal|WebSocket.acceptException|System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'System.Net.Sockets.NetworkStream'.
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)

Service.cs:56   [ERROR]         Client: An exception has occurred while receiving a message.
09.05.2014 15:18:24|Fatal|WebSocket.acceptException|System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback,Object state)
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)

--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at WebSocketSharp.Ext.ReadBytesAsync(Stream stream,Int32 length, Action`1 completed, Action`1 error)
at WebSocketSharp.WsFrame.ParseAsync(Stream stream,Boolean unmask, Action`1 completed, Action`1 error)
at WebSocketSharp.WsStream.ReadFrameAsync(Action`1completed, Action`1 error)
at WebSocketSharp.WebSocket.<>c__DisplayClass18.<startReceiving>b__15()
at WebSocketSharp.WebSocket.<>c__DisplayClass18.<startReceiving>b__16(WsFrame frame)
at WebSocketSharp.WsFrame.<>c__DisplayClasse.<ParseAsync>b__d(Byte[] header)
at WebSocketSharp.Ext.<>c__DisplayClass6.<ReadBytesAsync>b__5(IAsyncResult ar)
Service.cs:56   [ERROR]         Client: The WebSocket connection has already been closed.

I'm connecting with the collowing code

            Stopwatch watch = new Stopwatch();
            watch.Start();

            ws = new WebSocket("ws://localhost:8140/game");

            ws.OnMessage += (sender, e) =>
                this.Progress(e);

            ws.OnError += (sender, e) =>
                Console.WriteLine("Stopwatch: " + watch.ElapsedMilliseconds);

            ws.OnClose += (sender, e) =>
                Console.WriteLine("Stopwatch: " + watch.ElapsedMilliseconds);

            ws.Connect();
            this.Login(hash);

And my server looks like this:

            GameEngine gameEngine = new GameEngine();
            ChatEngine chatEngine = new ChatEngine();

            wssv.AddWebSocketService<GameService>("/game", () => new GameService(gameEngine));
            wssv.AddWebSocketService<ChatService>("/chat", () => new ChatService(chatEngine));
            wssv.AddWebSocketService<AdminService>("/admin", () => new AdminService(gameEngine, chatEngine));
            wssv.Log.Level = LogLevel.Fatal;

            wssv.Start();

            Console.ReadKey(true);
            wssv.Stop();

I logged duration of the connection three times, and i got the following timings: 57835ms First run 56207ms Second run 57343ms Third run

I'm not sure if this is something with websocket-sharp, but maybe you know the problem? :+1:

sta commented 10 years ago

Hi there, Could you add the following to your server code before wssv.Start();?

wssv.KeepClean = false;
perara commented 10 years ago

Hello

This seems to work perfectly!

Is this a intended behaviour?

Thanks!

sta commented 10 years ago

Is this a intended behaviour?

Nope. I have another workaround, so plz wait for a short while. Thx!

perara commented 10 years ago

+1

253153 commented 6 years ago

Is this workaround still needed? I am running into the same issue on .NET Core 2.1.