From 7aea0b98307a6b8b8070dc17722cf067cc8daaeb Mon Sep 17 00:00:00 2001 From: hujiebin Date: Fri, 28 Jul 2023 14:38:13 +0800 Subject: [PATCH] Feature/body size --- common/coder.go | 4 ++ main.go | 120 ++++++++++++++++++++++++++++++++++++++ protocol/userCenter.proto | 30 ++++++++++ protocol/userProxy.proto | 52 +++++++++++++++++ 4 files changed, 206 insertions(+) diff --git a/common/coder.go b/common/coder.go index 46b3a65..ca331b9 100644 --- a/common/coder.go +++ b/common/coder.go @@ -69,6 +69,10 @@ const ( MsgTypeMatchV2Confirm = 141 // 匹配-v2-确认 MsgTypeMatchV2CallReady = 142 // 匹配-v2-callReady MsgTypeMatchV2AddTimeGift = 143 // 匹配-v2-送礼加时长 + MsgTypeEnterRoom = 152 // 用户进房 + MsgTypeLeaveRoom = 153 // 用户离房 + MsgTypeRoomHeartbeat = 154 // 房间心跳 + MsgTypeGroupMicChangeRsp = 156 // 麦位变化确认 ) const ( diff --git a/main.go b/main.go index 92e3647..b6c5697 100644 --- a/main.go +++ b/main.go @@ -241,6 +241,22 @@ func serverWebsocket(w http.ResponseWriter, r *http.Request) { } } } + } else if msgType == common.MsgTypeEnterRoom { + if err := processEnterRoom(logger, ci.Uid, pbData); err != nil { + logger.Errorf("processEnterRoom fail:%v", err) + } + } else if msgType == common.MsgTypeLeaveRoom { + if err := processLeaveRoom(logger, ci.Uid, pbData); err != nil { + logger.Errorf("processLeaveRoom fail:%v", err) + } + } else if msgType == common.MsgTypeRoomHeartbeat { + if err := processRoomHeartbeat(logger, ci.Uid, pbData); err != nil { + logger.Errorf("processRoomHeartbeat fail:%v", err) + } + } else if msgType == common.MsgTypeGroupMicChangeRsp { + if err := processGroupMicChangeRsp(logger, ci.Uid, pbData); err != nil { + logger.Errorf("processGroupMicChangeRsp fail:%v", err) + } } else { logger.Warnf("Unknown message type %d", msgType) } @@ -401,6 +417,65 @@ func processBizRequest(logger *log.Entry, uid uint64, pbData []byte) (*userProxy } } +func processEnterRoom(logger *log.Entry, uid uint64, pbData []byte) error { + msg := &userProxy.EnterRoom{} + err := proto.Unmarshal(pbData, msg) + if err == nil { + err, status := doEnterRoom(uid, msg.GroupId) + if err == nil && status == common.Login_success { + logger.Infof("enter room success,uid:%v,group:%v", uid, msg.GroupId) + } else { + logger.Warnf("login RPC failed for %d, %v", status, err) + } + return err + } else { + return err + } +} + +func processLeaveRoom(logger *log.Entry, uid uint64, pbData []byte) error { + msg := &userProxy.LeaveRoom{} + err := proto.Unmarshal(pbData, msg) + if err == nil { + err, status := doLeaveRoom(uid, msg.GroupId) + if err == nil && status == common.Login_success { + logger.Infof("leave room success,uid:%v,group:%v", uid, msg.GroupId) + } else { + logger.Warnf("login RPC failed for %d, %v", status, err) + } + return err + } else { + return err + } +} + +func processRoomHeartbeat(logger *log.Entry, uid uint64, pbData []byte) error { + msg := &userProxy.RoomHeartBeat{} + err := proto.Unmarshal(pbData, msg) + if err == nil { + err, status := doRoomHeartbeat(uid, msg.GroupId) + if err == nil && status == common.Login_success { + logger.Infof("room heartbeat success,uid:%v,group:%v", uid, msg.GroupId) + } else { + logger.Warnf("login RPC failed for %d, %v", status, err) + } + return err + } else { + return err + } +} + +func processGroupMicChangeRsp(logger *log.Entry, uid uint64, pbData []byte) error { + msg := &userProxy.GroupMicChangeRsp{} + err := proto.Unmarshal(pbData, msg) + if err == nil { + logger.Infof("groupMicChangeRsp,uid:%v,seqId:%v", uid, msg.SeqId) + } else { + return err + } + return err +} + // 直接发匹配成功消息,调试用 func homePage(w http.ResponseWriter, r *http.Request) { rsp := "Home Page" @@ -568,6 +643,51 @@ func doBizRequest(uid uint64, msgType uint32, payLoad string) (uint32, error) { } } +func doEnterRoom(uid uint64, groupId string) (error, uint32) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) + defer cancel() + + r, err := userClient.EnterRoom(ctx, &userCenter.EnterRoomMessage{ + Uid: uid, + GroupId: groupId, + }) + if err == nil && r != nil { + return err, r.Status + } else { + return err, 0 + } +} + +func doLeaveRoom(uid uint64, groupId string) (error, uint32) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) + defer cancel() + + r, err := userClient.LeaveRoom(ctx, &userCenter.LeaveRoomMessage{ + Uid: uid, + GroupId: groupId, + }) + if err == nil && r != nil { + return err, r.Status + } else { + return err, 0 + } +} + +func doRoomHeartbeat(uid uint64, groupId string) (error, uint32) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) + defer cancel() + + r, err := userClient.RoomHeartbeat(ctx, &userCenter.RoomHeartbeatMessage{ + Uid: uid, + GroupId: groupId, + }) + if err == nil && r != nil { + return err, r.Status + } else { + return err, 0 + } +} + var kacp = keepalive.ClientParameters{ Time: 10 * time.Second, // send pings every 10 seconds if there is no activity Timeout: time.Second, // wait 1 second for ping ack before considering the connection dead diff --git a/protocol/userCenter.proto b/protocol/userCenter.proto index 10535b3..644f0a4 100644 --- a/protocol/userCenter.proto +++ b/protocol/userCenter.proto @@ -85,6 +85,33 @@ message BizMessageRsp { uint32 status = 1; } +message EnterRoomMessage { + uint64 uid = 1; + string groupId = 2; +} + +message EnterRoomMessageRsp { + uint32 status = 1; +} + +message LeaveRoomMessage { + uint64 uid = 1; + string groupId = 2; +} + +message LeaveRoomMessageRsp { + uint32 status = 1; +} + +message RoomHeartbeatMessage { + uint64 uid = 1; + string groupId = 2; +} + +message RoomHeartbeatMessageRsp { + uint32 status = 1; +} + service Router { rpc route(RouteMessage) returns (RouteMessageRsp) {} rpc kickUser(KickMessage) returns (KickMessageRsp) {} @@ -96,4 +123,7 @@ service User { rpc multicast(MulticastMessage) returns (MulticastMessageRsp) {} rpc broadcast(BroadcastMessage) returns (BroadcastMessageRsp) {} rpc transmit(BizMessage) returns (BizMessageRsp) {} + rpc enterRoom(EnterRoomMessage) returns (EnterRoomMessageRsp) {} + rpc leaveRoom(LeaveRoomMessage) returns (LeaveRoomMessageRsp) {} + rpc roomHeartbeat(RoomHeartbeatMessage) returns (RoomHeartbeatMessageRsp) {} } \ No newline at end of file diff --git a/protocol/userProxy.proto b/protocol/userProxy.proto index 85e540e..906b1d6 100644 --- a/protocol/userProxy.proto +++ b/protocol/userProxy.proto @@ -411,4 +411,56 @@ message SvipUpgrade { User user = 1; uint32 svip_level = 2; string group_id = 3; +} + +/* id == 152 用户进房 上行 */ +message EnterRoom { + string group_id = 1; +} + +/* id == 153 用户离房 上行 */ +message LeaveRoom { + string group_id = 1; +} + +/* id == 154 房间心跳 上行 */ +message RoomHeartBeat { + string group_id = 1; +} + +/* id == 155 麦位变化 下行 */ +message GroupMicChange { + string seqId = 1; + string group_id = 2; + uint32 i = 3; + bool lock = 4; + bool forbid = 5; + bool micForbid = 6; + string externalId = 7; + uint32 agoraId = 8; + int64 timestamp = 9; + MicUserData user = 10; +} + +/* id == 156 麦位变化确认 上行 */ +message GroupMicChangeRsp { + string seqId = 1; +} + +message MicUserData { + uint64 id = 1; + string externalId = 2; + string avatar = 3; + string nick = 4; + uint32 sex = 5; + string code = 6; + bool isVip = 7; + uint32 noble = 8; + string headwearPicUrl = 9; + string headwearEffectUrl = 10; + string headwearReverseEffectUrl = 11; + uint32 svipLevel = 12; + string micEffect = 14; + string headwearIcon = 15; + Svip svip = 16; } \ No newline at end of file -- 2.22.0