Realtime Notify

This commit is contained in:
2024-03-31 16:03:59 +08:00
parent 7873bafa4f
commit e8aac7bb66
11 changed files with 140 additions and 72 deletions

View File

@ -5,8 +5,12 @@ import (
"github.com/gofiber/contrib/websocket"
)
type WsPushRequest struct {
Payload []byte
RecipientID uint
}
var WsConn = make(map[uint][]*websocket.Conn)
var WsNotifyQueue = make(map[uint][]byte)
func CheckOnline(user models.Account) bool {
return len(WsConn[user.ID]) > 0

View File

@ -31,14 +31,19 @@ func NewNotification(notification models.Notification) error {
go func() {
err := PushNotification(notification)
log.Error().Err(err).Msg("Unexpected error occurred during the notification.")
if err != nil {
log.Error().Err(err).Msg("Unexpected error occurred during the notification.")
}
}()
return nil
}
func PushNotification(notification models.Notification) error {
WsNotifyQueue[notification.RecipientID], _ = jsoniter.Marshal(notification)
raw, _ := jsoniter.Marshal(notification)
for _, conn := range WsConn[notification.RecipientID] {
_ = conn.WriteMessage(1, raw)
}
var subscribers []models.NotificationSubscriber
if err := database.C.Where(&models.NotificationSubscriber{