close the client tcp socket on the server

master
Jordan Orelli 4 years ago
parent 455dbd8a0c
commit cbee897739

@ -18,9 +18,8 @@ import (
type Server struct { type Server struct {
*blammo.Log *blammo.Log
Host string Host string
Port int Port int
lastSessionID int lastSessionID int
} }
@ -50,6 +49,7 @@ func (s *Server) Start() error {
return fmt.Errorf("server failed to start a listener: %w", err) return fmt.Errorf("server failed to start a listener: %w", err)
} }
s.Log.Info("listening for TCP traffic on %q", addr) s.Log.Info("listening for TCP traffic on %q", addr)
go s.runHTTPServer(lis) go s.runHTTPServer(lis)
return nil return nil
@ -71,6 +71,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return 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()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()

@ -112,6 +112,13 @@ func (c *Client) readLoop(notifications chan<- Response) {
} }
} }
c.done <- false 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() { func (c *Client) writeLoop() {
@ -158,6 +165,8 @@ func (c *Client) writeLoop() {
close(p.done) close(p.done)
case shouldClose := <-c.done: case shouldClose := <-c.done:
c.Info("write loop sees done signal: %t", shouldClose)
if shouldClose { if shouldClose {
c.Info("sending close frame") c.Info("sending close frame")
msg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "") msg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
@ -166,7 +175,6 @@ func (c *Client) writeLoop() {
} else { } else {
c.Info("sent close frame") c.Info("sent close frame")
} }
c.conn = nil
} }
return return
} }

Loading…
Cancel
Save