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 (
"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()
}
}

View File

@ -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)
}
}