json, gob 둘다 지원
This commit is contained in:
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user