Nexus/pkg/internal/web/ws/connections.go

128 lines
2.9 KiB
Go
Raw Normal View History

2024-10-20 19:04:41 +08:00
package ws
2024-10-19 22:36:33 +08:00
import (
2025-03-01 14:51:53 +08:00
"fmt"
"sync"
2024-10-21 00:05:40 +08:00
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
2025-03-01 14:51:53 +08:00
"github.com/google/uuid"
"github.com/rs/zerolog/log"
2024-10-19 22:36:33 +08:00
"github.com/gofiber/contrib/websocket"
)
var (
wsMutex sync.Mutex
2025-03-01 14:51:53 +08:00
wsConn = make(map[uint]map[string]*websocket.Conn)
2024-10-19 22:36:33 +08:00
)
2025-03-01 14:51:53 +08:00
func ClientRegister(user sec.UserInfo, conn *websocket.Conn) (string, error) {
2024-10-19 22:36:33 +08:00
wsMutex.Lock()
if wsConn[user.ID] == nil {
2025-03-01 14:51:53 +08:00
wsConn[user.ID] = make(map[string]*websocket.Conn)
}
var clientId string
if userDefinedId := conn.Query("clientId"); len(userDefinedId) > 0 {
clientId = userDefinedId
} else {
clientId = uuid.NewString()
}
if _, ok := wsConn[user.ID][clientId]; ok {
return clientId, fmt.Errorf("client already conncted")
2024-10-19 22:36:33 +08:00
}
wsConn[user.ID][clientId] = conn
wsMutex.Unlock()
log.Debug().
2025-03-01 14:51:53 +08:00
Str("client_id", clientId).
Uint("user_id", user.ID).
Msg("An client connected to stream endpoint...")
_ = directory.BroadcastEvent("ws.client.register", map[string]any{
2024-10-21 00:05:40 +08:00
"user": user.ID,
"id": clientId,
})
2025-03-01 14:51:53 +08:00
return clientId, nil
2024-10-19 22:36:33 +08:00
}
2025-03-01 14:51:53 +08:00
func ClientUnregister(user sec.UserInfo, id string) {
2024-10-19 22:36:33 +08:00
wsMutex.Lock()
if wsConn[user.ID] == nil {
2025-03-01 14:51:53 +08:00
wsConn[user.ID] = make(map[string]*websocket.Conn)
2024-10-19 22:36:33 +08:00
}
delete(wsConn[user.ID], id)
wsMutex.Unlock()
2024-10-21 00:05:40 +08:00
log.Debug().
2025-03-01 14:51:53 +08:00
Str("client_id", id).
Uint("user_id", user.ID).
Msg("An client disconnected from stream endpoint...")
_ = directory.BroadcastEvent("ws.client.unregister", map[string]any{
2024-10-21 00:05:40 +08:00
"user": user.ID,
"id": id,
})
2024-10-19 22:36:33 +08:00
}
func ClientCount(uid uint) int {
return len(wsConn[uid])
}
2025-03-01 14:51:53 +08:00
func WebsocketPush(uid uint, body []byte) (count int, successes []string, errs []error) {
2024-10-19 22:36:33 +08:00
for _, conn := range wsConn[uid] {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
2025-03-01 14:51:53 +08:00
successes = append(successes, fmt.Sprintf("%d", uid))
2024-10-19 22:36:33 +08:00
}
count++
}
return
}
2025-03-01 14:51:53 +08:00
func WebsocketPushDirect(clientId string, body []byte) (count int, successes []string, errs []error) {
2024-10-19 22:36:33 +08:00
for _, m := range wsConn {
if conn, ok := m[clientId]; ok {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
successes = append(successes, clientId)
2024-10-19 22:36:33 +08:00
}
count++
}
}
return
}
2025-03-01 14:51:53 +08:00
func WebsocketPushBatch(uidList []uint, body []byte) (count int, successes []string, errs []error) {
2024-10-19 22:36:33 +08:00
for _, uid := range uidList {
for _, conn := range wsConn[uid] {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
2025-03-01 14:51:53 +08:00
successes = append(successes, fmt.Sprintf("%d", uid))
2024-10-19 22:36:33 +08:00
}
count++
}
}
return
}
2025-03-01 14:51:53 +08:00
func WebsocketPushBatchDirect(clientIdList []string, body []byte) (count int, successes []string, errs []error) {
2024-10-19 22:36:33 +08:00
for _, clientId := range clientIdList {
for _, m := range wsConn {
if conn, ok := m[clientId]; ok {
if err := conn.WriteMessage(1, body); err != nil {
errs = append(errs, err)
} else {
successes = append(successes, clientId)
2024-10-19 22:36:33 +08:00
}
count++
}
}
}
return
}