diff --git a/internal/websocket/handle_connection.go b/internal/websocket/handle_connection.go index 9c4b43f..46ce71d 100644 --- a/internal/websocket/handle_connection.go +++ b/internal/websocket/handle_connection.go @@ -5,6 +5,7 @@ import ( "net" "net/http" "sync" + "time" "git.smsvc.net/pomodoro/GoTomato/internal/helper" "git.smsvc.net/pomodoro/GoTomato/pkg/models" @@ -34,6 +35,12 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) { Conn: ws, RealIP: r.RemoteAddr, } + + client.Conn.SetPongHandler(func(s string) error { + client.LastPong = time.Now() + return nil + }) + mu.Lock() Clients[ws.LocalAddr()] = &client mu.Unlock() diff --git a/pkg/models/client.go b/pkg/models/client.go index a758e77..41f5adf 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -25,10 +25,6 @@ type WebsocketClient struct { // Sends a message to the websocket. // Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time. func (c *WebsocketClient) SendMessage(messageType int, data []byte) error { - c.Conn.SetPongHandler(func(s string) error { - c.LastPong = time.Now() - return nil - }) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) c.Conn.WriteMessage(websocket.PingMessage, nil)