improve websocket ping and remodel for rename datapoint

This commit is contained in:
Adrian Zuercher
2025-07-25 18:26:36 +02:00
parent a23f82e9fe
commit c3a3060129
11 changed files with 231 additions and 92 deletions

View File

@@ -38,6 +38,7 @@ func (cH *ClientHandler) ConnectNewClient(id string, c *gin.Context) (client *mo
client, err = models.ConnectNewClient(id, c)
client.OnClose = func(code int, reason string) {
fmt.Println(23, "closing", id)
delete(cH.Clients, id)
}

View File

@@ -17,10 +17,10 @@ var Broadcast Clients = make(Clients)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
writeWait = 30 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 10 * time.Second
pongWait = 30 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
@@ -34,6 +34,7 @@ type Client struct {
OnMessage func(data []byte)
OnClose func(code int, reason string)
OnError func(err error)
OnWarning func(warn string)
OnPing func()
OnPong func()
send chan []byte
@@ -177,7 +178,15 @@ func (c *Client) SendResponse(data []byte) {
if !c.Connected {
return
}
c.send <- data
select {
case c.send <- data:
// sent successfully
default:
// channel full, drop or log
if c.OnWarning != nil {
c.OnWarning("Dropping message: channel full")
}
}
}
func (c *Client) Close(code int, reason string) error {