From c86730cabc7f4fca17637e287f084c623c57efad Mon Sep 17 00:00:00 2001 From: Milarin Date: Thu, 15 Feb 2024 17:55:32 +0100 Subject: [PATCH] discard client input after onNewClient callback finished --- server.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/server.go b/server.go index 209f67b..78bfd78 100644 --- a/server.go +++ b/server.go @@ -30,14 +30,6 @@ func Listen(socketPath string, onNewClient func(client net.Conn)) (*Server, erro return nil, err } - if onNewClient == nil { - onNewClient = func(client net.Conn) { - data := make([]byte, 1024) - for _, err := client.Read(data); err == nil; _, err = client.Read(data) { - } - } - } - s := &Server{ socketPath: absPath, server: server, @@ -70,7 +62,14 @@ func (s *Server) handleClients() { func (s *Server) handleClient(client net.Conn) { s.clients.Put(client, struct{}{}) defer s.clients.Delete(client) - s.onNewClient(client) + + if s.onNewClient != nil { + s.onNewClient(client) + } + + data := make([]byte, 1024) + for _, err := client.Read(data); err == nil; _, err = client.Read(data) { + } } func (s *Server) Close() error {