diff --git a/wshandler/room.go b/wshandler/room.go index 8a1c046..c04a946 100644 --- a/wshandler/room.go +++ b/wshandler/room.go @@ -2,6 +2,7 @@ package wshandler import ( "context" + "encoding/json" "github.com/gorilla/websocket" "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 { select { 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()) 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 { writer, _ := conn.NextWriter(websocket.TextMessage) - writer.Write(a) - writer.Write([]byte(msg.Alias)) - writer.Write(b) - writer.Write(msg.Body) - writer.Write(c) + writer.Write(bt) writer.Close() } } diff --git a/wshandler/wshandler.go b/wshandler/wshandler.go index 58f54b8..e4427c0 100644 --- a/wshandler/wshandler.go +++ b/wshandler/wshandler.go @@ -31,13 +31,13 @@ type UpstreamMessage struct { Alias string Accid primitive.ObjectID Target string - Body []byte + Body any Tag []string } type DownstreamMessage struct { Alias string `json:",omitempty"` - Body string `json:",omitempty"` + Body any `json:",omitempty"` Tag []string `json:",omitempty"` } @@ -314,7 +314,13 @@ func (sh *subhandler) mainLoop(ctx context.Context) { conn := entireConns[accid] if conn != nil { // 이 경우 아니면 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 } } @@ -370,7 +376,12 @@ func (sh *subhandler) mainLoop(ctx context.Context) { accid := target[1:] conn := entireConns[accid] 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) } }