diff --git a/server.go b/server.go index d3f98f8..3762acb 100644 --- a/server.go +++ b/server.go @@ -430,17 +430,44 @@ func ReadStringFormValue(r url.Values, key string) (string, bool) { return strval, len(strval) > 0 } -func DecodeGob[T any](r io.Reader, out *T) error { - dec := gob.NewDecoder(r) - return dec.Decode(out) +type encoder interface { + Encode(any) error } -func ReadJsonDocumentFromBody[T any](r io.Reader, out *T) error { - bt, err := io.ReadAll(r) - if err != nil { - return err +type nilEncoder struct{} + +func (ne *nilEncoder) Encode(any) error { return nil } + +type decoder interface { + Decode(any) error +} + +type nilDecoder struct{} + +func (nd *nilDecoder) Decode(any) error { return nil } + +func MakeDecoder(r *http.Request) decoder { + ct := r.Header.Get("Content-Type") + if ct == "application/gob" { + return gob.NewDecoder(r.Body) + } else if ct == "application/json" { + return json.NewDecoder(r.Body) } - return json.Unmarshal(bt, out) + + logger.Error("Content-Type is not supported :", ct) + return &nilDecoder{} +} + +func MakeEncoder(w http.ResponseWriter, r *http.Request) encoder { + ct := r.Header.Get("Content-Type") + if ct == "application/gob" { + return gob.NewEncoder(w) + } else if ct == "application/json" { + return json.NewEncoder(w) + } + + logger.Error("Content-Type is not supported :", ct) + return &nilEncoder{} } func DotStringToTimestamp(tv string) primitive.Timestamp { diff --git a/wshandler/room.go b/wshandler/room.go index c26bbf0..b807e42 100644 --- a/wshandler/room.go +++ b/wshandler/room.go @@ -6,12 +6,13 @@ import ( "encoding/json" "github.com/gorilla/websocket" + "go.mongodb.org/mongo-driver/bson/primitive" "repositories.action2quare.com/ayo/gocommon/logger" ) type room struct { inChan chan *wsconn - outChan chan *wsconn + outChan chan primitive.ObjectID messageChan chan *UpstreamMessage name string destroyChan chan<- string @@ -22,7 +23,7 @@ type room struct { func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room { return &room{ inChan: make(chan *wsconn, 10), - outChan: make(chan *wsconn, 10), + outChan: make(chan primitive.ObjectID, 10), messageChan: make(chan *UpstreamMessage, 100), name: name, destroyChan: destroyChan, @@ -39,8 +40,8 @@ func (r *room) in(conn *wsconn) *room { return r } -func (r *room) out(conn *wsconn) *room { - r.outChan <- conn +func (r *room) out(accid primitive.ObjectID) *room { + r.outChan <- accid return r } @@ -72,8 +73,8 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b case conn := <-r.inChan: (*conns)[conn.sender.Accid.Hex()] = conn - case conn := <-r.outChan: - delete((*conns), conn.sender.Accid.Hex()) + case accid := <-r.outChan: + delete((*conns), accid.Hex()) if len(*conns) == 0 && r.destroyChan != nil { r.destroyChan <- r.name return true diff --git a/wshandler/wshandler.go b/wshandler/wshandler.go index 10ba250..f4e4755 100644 --- a/wshandler/wshandler.go +++ b/wshandler/wshandler.go @@ -130,7 +130,7 @@ func NewWebsocketHandler(consumer session.Consumer, redisUrl string) (*Websocket defer func() { r := recover() if r != nil { - logger.Println(r) + logger.Println("send_msg_queue_elem sender recover :", r, string(elem.msg)) } }() elem.to.WriteMessage(elem.mt, elem.msg) @@ -327,17 +327,11 @@ func (ws *WebsocketHandler) mainLoop(ctx context.Context) { } roomName := usermsg.Args[0].(string) accid := usermsg.Args[1].(primitive.ObjectID) - conn := entireConns[accid.Hex()] - if conn == nil { - return false, nil - } - room := findRoom(roomName, false) if room == nil { return false, errProcessFailed_NotInRoom } - - room.out(conn) + room.out(accid) } return true, nil }