close the client tcp socket on the server

master
Jordan Orelli 4 years ago
parent 455dbd8a0c
commit cbee897739

@ -20,7 +20,6 @@ type Server struct {
*blammo.Log
Host string
Port int
lastSessionID int
}
@ -50,6 +49,7 @@ func (s *Server) Start() error {
return fmt.Errorf("server failed to start a listener: %w", err)
}
s.Log.Info("listening for TCP traffic on %q", addr)
go s.runHTTPServer(lis)
return nil
@ -71,6 +71,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
defer func() {
s.Info("closing connection")
if err := conn.Close(); err != nil {
s.Error("error closing connection: %v", err)
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

@ -112,6 +112,13 @@ func (c *Client) readLoop(notifications chan<- Response) {
}
}
c.done <- false
c.Info("closing connection")
if err := c.conn.Close(); err != nil {
c.Error("error on connection close: %v", err)
} else {
c.Info("closed connection cleanly")
}
}
func (c *Client) writeLoop() {
@ -158,6 +165,8 @@ func (c *Client) writeLoop() {
close(p.done)
case shouldClose := <-c.done:
c.Info("write loop sees done signal: %t", shouldClose)
if shouldClose {
c.Info("sending close frame")
msg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
@ -166,7 +175,6 @@ func (c *Client) writeLoop() {
} else {
c.Info("sent close frame")
}
c.conn = nil
}
return
}

Loading…
Cancel
Save