✨ Realtime Notify
This commit is contained in:
@ -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
|
||||
|
@ -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{
|
||||
|
Reference in New Issue
Block a user