2024-03-31 05:04:48 +00:00
|
|
|
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"
|
2024-03-31 05:04:48 +00:00
|
|
|
"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
|
2024-03-31 05:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop connection
|
|
|
|
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool {
|
|
|
|
return item != c
|
|
|
|
})
|
|
|
|
}
|