json, gob 둘다 지원

This commit is contained in:
2023-09-11 12:45:15 +09:00
parent 23231dc6d7
commit 1af5d72819
3 changed files with 44 additions and 22 deletions

View File

@ -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
}
return json.Unmarshal(bt, out)
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)
}
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 {

View File

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

View File

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