Dealer/pkg/internal/server/api/ws.go

30 lines
671 B
Go
Raw Normal View History

2024-07-14 12:25:30 +00:00
package api
import (
"git.solsynth.dev/hydrogen/dealer/pkg/internal/models"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/services"
"github.com/gofiber/contrib/websocket"
"github.com/rs/zerolog/log"
)
func listenWebsocket(c *websocket.Conn) {
user := c.Locals("user").(models.Account)
// Push connection
services.ClientRegister(user, c)
log.Debug().Uint("user", user.ID).Msg("New websocket connection established...")
// Event loop
var err error
for {
if _, _, err = c.ReadMessage(); err != nil {
break
}
}
// Pop connection
services.ClientUnregister(user, c)
log.Debug().Uint("user", user.ID).Msg("A websocket connection disconnected...")
}