2024-07-14 12:25:30 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-07-20 11:47:04 +00:00
|
|
|
"context"
|
2024-08-23 11:08:07 +00:00
|
|
|
"math/rand"
|
2024-07-17 03:58:51 +00:00
|
|
|
"sync"
|
|
|
|
|
2024-07-20 11:47:04 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory"
|
2024-07-14 12:25:30 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/internal/models"
|
2024-07-20 11:47:04 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
2024-07-14 12:25:30 +00:00
|
|
|
"github.com/gofiber/contrib/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
wsMutex sync.Mutex
|
2024-08-23 11:08:07 +00:00
|
|
|
wsConn = make(map[uint]map[uint64]*websocket.Conn)
|
2024-07-14 12:25:30 +00:00
|
|
|
)
|
|
|
|
|
2024-08-23 11:08:07 +00:00
|
|
|
func ClientRegister(user models.Account, conn *websocket.Conn) uint64 {
|
2024-07-14 12:25:30 +00:00
|
|
|
wsMutex.Lock()
|
|
|
|
if wsConn[user.ID] == nil {
|
2024-08-23 11:08:07 +00:00
|
|
|
wsConn[user.ID] = make(map[uint64]*websocket.Conn)
|
2024-07-14 12:25:30 +00:00
|
|
|
}
|
2024-08-23 11:08:07 +00:00
|
|
|
clientId := rand.Uint64()
|
|
|
|
wsConn[user.ID][clientId] = conn
|
2024-07-14 12:25:30 +00:00
|
|
|
wsMutex.Unlock()
|
2024-07-20 11:47:04 +00:00
|
|
|
|
|
|
|
pc, err := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider).GetGrpcConn()
|
|
|
|
if err == nil {
|
|
|
|
proto.NewStreamControllerClient(pc).EmitStreamEvent(context.Background(), &proto.StreamEventRequest{
|
|
|
|
Event: "ClientRegister",
|
|
|
|
UserId: uint64(user.ID),
|
|
|
|
})
|
|
|
|
}
|
2024-08-23 11:08:07 +00:00
|
|
|
|
|
|
|
return clientId
|
2024-07-14 12:25:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-23 11:08:07 +00:00
|
|
|
func ClientUnregister(user models.Account, id uint64) {
|
2024-07-14 12:25:30 +00:00
|
|
|
wsMutex.Lock()
|
|
|
|
if wsConn[user.ID] == nil {
|
2024-08-23 11:08:07 +00:00
|
|
|
wsConn[user.ID] = make(map[uint64]*websocket.Conn)
|
2024-07-14 12:25:30 +00:00
|
|
|
}
|
2024-08-23 11:08:07 +00:00
|
|
|
delete(wsConn[user.ID], id)
|
2024-07-14 12:25:30 +00:00
|
|
|
wsMutex.Unlock()
|
2024-07-20 11:47:04 +00:00
|
|
|
|
|
|
|
pc, err := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider).GetGrpcConn()
|
|
|
|
if err == nil {
|
|
|
|
proto.NewStreamControllerClient(pc).EmitStreamEvent(context.Background(), &proto.StreamEventRequest{
|
|
|
|
Event: "ClientUnregister",
|
|
|
|
UserId: uint64(user.ID),
|
|
|
|
})
|
|
|
|
}
|
2024-07-14 12:25:30 +00:00
|
|
|
}
|
2024-07-14 15:45:18 +00:00
|
|
|
|
2024-07-14 15:49:34 +00:00
|
|
|
func ClientCount(uid uint) int {
|
|
|
|
return len(wsConn[uid])
|
|
|
|
}
|
|
|
|
|
2024-07-14 15:45:18 +00:00
|
|
|
func WebsocketPush(uid uint, body []byte) (count int, success int, errs []error) {
|
2024-08-23 11:08:07 +00:00
|
|
|
for _, conn := range wsConn[uid] {
|
2024-07-14 15:45:18 +00:00
|
|
|
if err := conn.WriteMessage(1, body); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
} else {
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-07-17 03:58:51 +00:00
|
|
|
|
2024-08-23 11:08:07 +00:00
|
|
|
func WebsocketPushDirect(clientId uint64, body []byte) (count int, success int, errs []error) {
|
|
|
|
for _, m := range wsConn {
|
|
|
|
if conn, ok := m[clientId]; ok {
|
|
|
|
if err := conn.WriteMessage(1, body); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
} else {
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-17 03:58:51 +00:00
|
|
|
func WebsocketPushBatch(uidList []uint, body []byte) (count int, success int, errs []error) {
|
|
|
|
for _, uid := range uidList {
|
2024-08-23 11:08:07 +00:00
|
|
|
for _, conn := range wsConn[uid] {
|
2024-07-17 03:58:51 +00:00
|
|
|
if err := conn.WriteMessage(1, body); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
} else {
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-08-23 11:08:07 +00:00
|
|
|
|
|
|
|
func WebsocketPushBatchDirect(clientIdList []uint64, body []byte) (count int, success int, errs []error) {
|
|
|
|
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 {
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|