statianzo / Fleck

C# Websocket Implementation
MIT License
2.25k stars 583 forks source link

Slow Connection Times? #322

Open LukeSawyers opened 2 years ago

LukeSawyers commented 2 years ago

Consistently experiencing rather long connection times from various clients (~ 2 sec) when using the fleck server Given Server code:

var s = new WebSocketServer("ws://0.0.0.0:8049")
s.ListenerSocket.NoDelay = true;
s.Start(HandleSocketConnection);

Where HandleSocketConnection can be an empty method, doesn't appear to make much of a difference And a variety of client implementations:

using var fleckServer = CreateServer();

var sw = Stopwatch.StartNew();
var client = await CreateClient();
_logger.Info($"Net Client Connect time {sw.ElapsedMilliseconds}ms");
sw.Restart();
var socket = new WebSocketSharp.WebSocket(RootUrl + Endpoint1);
socket.Connect();
_logger.Info($"Socket sharp connect time {sw.ElapsedMilliseconds}ms");
sw.Restart();
await new ClientWebSocket().ConnectAsync(new Uri(RootUrl + Endpoint1), CancellationToken.None);
_logger.Info($"Client Web Socket connect time {sw.ElapsedMilliseconds}ms");

Output shows some quite long connection times

Debug: Socket open to /endpoint1 from 127.0.0.1:63778 
Info: Websocket Reconnected : Initial 
Info: Net Client Connect time 3105ms 
Debug: Socket open to /endpoint1 from 127.0.0.1:63781 
Info: Socket sharp connect time 2035ms 
Debug: Socket open to /endpoint1 from 127.0.0.1:63782 
Info: Client Web Socket connect time 2043ms 
Info: Websocket Disconnected : ByServer NormalClosure

Running on .NET 5.0 and 6.0 with similar results

Normally under the same circumstances I'd expect to see < 100 ms. These times seem a bit too slow. Is there something I'm missing / can this be replicated?