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