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 return strval, len(strval) > 0
} }
func DecodeGob[T any](r io.Reader, out *T) error { type encoder interface {
dec := gob.NewDecoder(r) Encode(any) error
return dec.Decode(out)
} }
func ReadJsonDocumentFromBody[T any](r io.Reader, out *T) error { type nilEncoder struct{}
bt, err := io.ReadAll(r)
if err != nil { func (ne *nilEncoder) Encode(any) error { return nil }
return err
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 { func DotStringToTimestamp(tv string) primitive.Timestamp {

View File

@ -6,12 +6,13 @@ import (
"encoding/json" "encoding/json"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
) )
type room struct { type room struct {
inChan chan *wsconn inChan chan *wsconn
outChan chan *wsconn outChan chan primitive.ObjectID
messageChan chan *UpstreamMessage messageChan chan *UpstreamMessage
name string name string
destroyChan chan<- 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 { func makeRoom(name string, destroyChan chan<- string, sendMsgChan chan<- send_msg_queue_elem) *room {
return &room{ return &room{
inChan: make(chan *wsconn, 10), inChan: make(chan *wsconn, 10),
outChan: make(chan *wsconn, 10), outChan: make(chan primitive.ObjectID, 10),
messageChan: make(chan *UpstreamMessage, 100), messageChan: make(chan *UpstreamMessage, 100),
name: name, name: name,
destroyChan: destroyChan, destroyChan: destroyChan,
@ -39,8 +40,8 @@ func (r *room) in(conn *wsconn) *room {
return r return r
} }
func (r *room) out(conn *wsconn) *room { func (r *room) out(accid primitive.ObjectID) *room {
r.outChan <- conn r.outChan <- accid
return r return r
} }
@ -72,8 +73,8 @@ func (r *room) loop(ctx context.Context, conns *map[string]*wsconn) (normalEnd b
case conn := <-r.inChan: case conn := <-r.inChan:
(*conns)[conn.sender.Accid.Hex()] = conn (*conns)[conn.sender.Accid.Hex()] = conn
case conn := <-r.outChan: case accid := <-r.outChan:
delete((*conns), conn.sender.Accid.Hex()) delete((*conns), accid.Hex())
if len(*conns) == 0 && r.destroyChan != nil { if len(*conns) == 0 && r.destroyChan != nil {
r.destroyChan <- r.name r.destroyChan <- r.name
return true return true

View File

@ -130,7 +130,7 @@ func NewWebsocketHandler(consumer session.Consumer, redisUrl string) (*Websocket
defer func() { defer func() {
r := recover() r := recover()
if r != nil { 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) elem.to.WriteMessage(elem.mt, elem.msg)
@ -327,17 +327,11 @@ func (ws *WebsocketHandler) mainLoop(ctx context.Context) {
} }
roomName := usermsg.Args[0].(string) roomName := usermsg.Args[0].(string)
accid := usermsg.Args[1].(primitive.ObjectID) accid := usermsg.Args[1].(primitive.ObjectID)
conn := entireConns[accid.Hex()]
if conn == nil {
return false, nil
}
room := findRoom(roomName, false) room := findRoom(roomName, false)
if room == nil { if room == nil {
return false, errProcessFailed_NotInRoom return false, errProcessFailed_NotInRoom
} }
room.out(accid)
room.out(conn)
} }
return true, nil return true, nil
} }