Passport/pkg/server/notify_ws.go

29 lines
624 B
Go
Raw Normal View History

package server
import (
2024-04-13 05:48:19 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/models"
"git.solsynth.dev/hydrogen/passport/pkg/services"
"github.com/gofiber/contrib/websocket"
"github.com/samber/lo"
)
func listenNotifications(c *websocket.Conn) {
user := c.Locals("principal").(models.Account)
// Push connection
services.WsConn[user.ID] = append(services.WsConn[user.ID], c)
// Event loop
var err error
for {
2024-03-31 08:03:59 +00:00
if _, _, err = c.ReadMessage(); err != nil {
break
}
}
// Pop connection
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool {
return item != c
})
}