message body를 any로 변경

This commit is contained in:
2023-07-11 14:30:10 +09:00
parent 9fd0dd00cb
commit 4acb81a20d
2 changed files with 24 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package wshandler
import ( import (
"context" "context"
"encoding/json"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
@ -54,8 +55,6 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b
} }
}() }()
a, b, c := []byte(`{"alias":"`), []byte(`","body":"`), []byte(`"}`)
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -68,13 +67,16 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b
delete((*conns), conn.sender.Accid.Hex()) delete((*conns), conn.sender.Accid.Hex())
case msg := <-r.messageChan: case msg := <-r.messageChan:
ds := DownstreamMessage{
Alias: msg.Alias,
Body: msg.Body,
Tag: append(msg.Tag, r.name),
}
bt, _ := json.Marshal(ds)
for _, conn := range *conns { for _, conn := range *conns {
writer, _ := conn.NextWriter(websocket.TextMessage) writer, _ := conn.NextWriter(websocket.TextMessage)
writer.Write(a) writer.Write(bt)
writer.Write([]byte(msg.Alias))
writer.Write(b)
writer.Write(msg.Body)
writer.Write(c)
writer.Close() writer.Close()
} }
} }

View File

@ -31,13 +31,13 @@ type UpstreamMessage struct {
Alias string Alias string
Accid primitive.ObjectID Accid primitive.ObjectID
Target string Target string
Body []byte Body any
Tag []string Tag []string
} }
type DownstreamMessage struct { type DownstreamMessage struct {
Alias string `json:",omitempty"` Alias string `json:",omitempty"`
Body string `json:",omitempty"` Body any `json:",omitempty"`
Tag []string `json:",omitempty"` Tag []string `json:",omitempty"`
} }
@ -314,7 +314,13 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
conn := entireConns[accid] conn := entireConns[accid]
if conn != nil { if conn != nil {
// 이 경우 아니면 publish 해야 함 // 이 경우 아니면 publish 해야 함
conn.WriteMessage(websocket.TextMessage, usermsg.Body) ds, _ := json.Marshal(DownstreamMessage{
Alias: usermsg.Alias,
Body: usermsg.Body,
Tag: usermsg.Tag,
})
conn.WriteMessage(websocket.TextMessage, ds)
break break
} }
} }
@ -370,7 +376,12 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
accid := target[1:] accid := target[1:]
conn := entireConns[accid] conn := entireConns[accid]
if conn != nil { if conn != nil {
conn.WriteMessage(websocket.TextMessage, usermsg.Body) ds, _ := json.Marshal(DownstreamMessage{
Alias: usermsg.Alias,
Body: usermsg.Body,
Tag: usermsg.Tag,
})
conn.WriteMessage(websocket.TextMessage, ds)
} }
} }