웹소켓 메시지 전송 채널 단일화

This commit is contained in:
2024-02-01 18:29:24 +09:00
parent d5e932de11
commit 40fb698748
5 changed files with 18 additions and 22 deletions

View File

@ -7,7 +7,6 @@ import (
"github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/gocommon/wshandler"
)
@ -20,6 +19,7 @@ type connWithFriends struct {
type connections struct {
connLock sync.Mutex
conns map[primitive.ObjectID]*connWithFriends
wsh *wshandler.WebsocketHandler
redison *gocommon.RedisonHandler
}
@ -90,12 +90,7 @@ func (cs *connections) writeMessage(acc primitive.ObjectID, src any) {
}
if bt, err := json.Marshal(src); err == nil {
pmsg, err := websocket.NewPreparedMessage(websocket.TextMessage, bt)
if err != nil {
logger.Println("connections.writeMessage failed :", err)
} else {
conn.c.WritePreparedMessage(pmsg)
}
cs.wsh.WriteDirectMessage(conn.c, websocket.TextMessage, bt)
}
}
@ -109,9 +104,10 @@ func (cs *connections) ClientDisconnected(msg string, callby *wshandler.Sender)
cs.delete(callby.Accid)
}
func makeConnections(redison *gocommon.RedisonHandler) *connections {
func makeConnections(redison *gocommon.RedisonHandler, wsh *wshandler.WebsocketHandler) *connections {
return &connections{
conns: make(map[primitive.ObjectID]*connWithFriends),
wsh: wsh,
redison: redison,
}
}