sta / websocket-sharp

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

Websocket-sharp-server: Ping sent delayed during heavy data transfer #418

Open namezis opened 6 years ago

namezis commented 6 years ago

When stress testing websocket-sharp-server with intensive data transfer (mostly from server to client), I have noticed that Ping (from server to the client), however executing "internal bool Ping (byte[] frameAsBytes, TimeSpan timeout)" and its "sendBytes(frameAsBytes)", does not actually sent the Ping for very long time. The time is so long that the Sweep terminates the socket soon afterwards.

My test is using short binary (14 bytes) messages (x 1000) passed via "public void Send (byte[] data)". Both "internal bool Ping(byte[] frameAsBytes, TimeSpan timeout)" and "private bool send(Opcode opcode, Stream stream)" finish in sendBytes(), which in turn writes to the same "_stream". If the stream is containing large number of data, then the appended "Ping frame" will wait until the buffer reaches it. In the meantime the Sweep fails. This would mean that the websocket cannot be used in heavy data usage, or I will have to wait on every (every some) call to Send, for the _stream to be empty. This to make sure the upcoming "Ping frame" has priority.

Is my observation correct? How to control the flow the best way, not to disturb the websocket protocol, and not to abuse the _stream?

I have code base marked as 1.0.2.* checkout 91 days ago.

GCat commented 6 years ago

any solution for this problem?

namezis commented 6 years ago

Yes mate. You have to implement flowcontrol (protocol of some kind) on top of websocket so the buffers are not overloaded.