move pong handler to connection handler
This commit is contained in:
parent
6f60423c03
commit
5f8c818118
2 changed files with 7 additions and 4 deletions
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue