commandMessage를 private으로 만듬

This commit is contained in:
2023-07-11 09:56:04 +09:00
parent 3bb985d0b6
commit a42eb2888e

View File

@ -39,16 +39,16 @@ type DownstreamMessage struct {
Body string Body string
} }
type CommandType string type commandType string
const ( const (
CommandType_JoinRoom = CommandType("join_room") commandType_JoinRoom = commandType("join_room")
CommandType_LeaveRoom = CommandType("leave_room") commandType_LeaveRoom = commandType("leave_room")
CommandType_WriteControl = CommandType("write_control") commandType_WriteControl = commandType("write_control")
) )
type CommandMessage struct { type commandMessage struct {
Cmd CommandType Cmd commandType
Args []any Args []any
} }
@ -210,8 +210,8 @@ func (ws *WebsocketHandler) SendUpstreamMessage(region string, msg *UpstreamMess
func (ws *WebsocketHandler) SendCloseMessage(region string, target string, text string) { func (ws *WebsocketHandler) SendCloseMessage(region string, target string, text string) {
sh := ws.authCaches[region] sh := ws.authCaches[region]
if sh != nil { if sh != nil {
sh.localDeliveryChan <- &CommandMessage{ sh.localDeliveryChan <- &commandMessage{
Cmd: CommandType_WriteControl, Cmd: commandType_WriteControl,
Args: []any{ Args: []any{
target, target,
int(websocket.CloseMessage), int(websocket.CloseMessage),
@ -221,6 +221,26 @@ func (ws *WebsocketHandler) SendCloseMessage(region string, target string, text
} }
} }
func (ws *WebsocketHandler) EnterRoom(region string, room string, accid primitive.ObjectID) {
sh := ws.authCaches[region]
if sh != nil {
sh.localDeliveryChan <- &commandMessage{
Cmd: commandType_JoinRoom,
Args: []any{room, accid},
}
}
}
func (ws *WebsocketHandler) LeaveRoom(region string, room string, accid primitive.ObjectID) {
sh := ws.authCaches[region]
if sh != nil {
sh.localDeliveryChan <- &commandMessage{
Cmd: commandType_LeaveRoom,
Args: []any{room, accid},
}
}
}
func (sh *subhandler) mainLoop(ctx context.Context) { func (sh *subhandler) mainLoop(ctx context.Context) {
defer func() { defer func() {
s := recover() s := recover()
@ -247,7 +267,7 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
logger.Println("decode UpstreamMessage failed :", err) logger.Println("decode UpstreamMessage failed :", err)
} }
} else if raw.Channel == sh.redisCmdChanName { } else if raw.Channel == sh.redisCmdChanName {
var cmd CommandMessage var cmd commandMessage
if err := json.Unmarshal([]byte(raw.Payload), &cmd); err == nil { if err := json.Unmarshal([]byte(raw.Payload), &cmd); err == nil {
sh.deliveryChan <- &cmd sh.deliveryChan <- &cmd
} else { } else {
@ -300,8 +320,8 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
sh.redisSync.Publish(context.Background(), sh.redisMsgChanName, bt).Result() sh.redisSync.Publish(context.Background(), sh.redisMsgChanName, bt).Result()
} }
case *CommandMessage: case *commandMessage:
if usermsg.Cmd == CommandType_JoinRoom && len(usermsg.Args) == 2 { if usermsg.Cmd == commandType_JoinRoom && len(usermsg.Args) == 2 {
accid := usermsg.Args[0].(string) accid := usermsg.Args[0].(string)
roomName := usermsg.Args[1].(string) roomName := usermsg.Args[1].(string)
@ -310,7 +330,7 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
findRoom(roomName, true).in(conn) findRoom(roomName, true).in(conn)
break break
} }
} else if usermsg.Cmd == CommandType_LeaveRoom && len(usermsg.Args) == 2 { } else if usermsg.Cmd == commandType_LeaveRoom && len(usermsg.Args) == 2 {
accid := usermsg.Args[0].(string) accid := usermsg.Args[0].(string)
roomName := usermsg.Args[1].(string) roomName := usermsg.Args[1].(string)
@ -321,7 +341,7 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
break break
} }
} }
} else if usermsg.Cmd == CommandType_WriteControl && len(usermsg.Args) == 2 { } else if usermsg.Cmd == commandType_WriteControl && len(usermsg.Args) == 2 {
accid := usermsg.Args[0].(string) accid := usermsg.Args[0].(string)
conn := entireConns[accid] conn := entireConns[accid]
if conn != nil { if conn != nil {
@ -354,8 +374,8 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
} }
} }
case *CommandMessage: case *commandMessage:
if usermsg.Cmd == CommandType_JoinRoom && len(usermsg.Args) == 2 { if usermsg.Cmd == commandType_JoinRoom && len(usermsg.Args) == 2 {
accid := usermsg.Args[0].(string) accid := usermsg.Args[0].(string)
roomName := usermsg.Args[1].(string) roomName := usermsg.Args[1].(string)
@ -363,7 +383,7 @@ func (sh *subhandler) mainLoop(ctx context.Context) {
if conn != nil { if conn != nil {
findRoom(roomName, true).in(conn) findRoom(roomName, true).in(conn)
} }
} else if usermsg.Cmd == CommandType_LeaveRoom && len(usermsg.Args) == 2 { } else if usermsg.Cmd == commandType_LeaveRoom && len(usermsg.Args) == 2 {
accid := usermsg.Args[0].(string) accid := usermsg.Args[0].(string)
roomName := usermsg.Args[1].(string) roomName := usermsg.Args[1].(string)