Commit 62f6240b authored by hujiebin's avatar hujiebin

feat:进房离房心跳

parent 01804402
......@@ -69,6 +69,9 @@ const (
MsgTypeMatchV2Confirm = 141 // 匹配-v2-确认
MsgTypeMatchV2CallReady = 142 // 匹配-v2-callReady
MsgTypeMatchV2AddTimeGift = 143 // 匹配-v2-送礼加时长
MsgTypeEnterRoom = 152 // 用户进房
MsgTypeLeaveRoom = 153 // 用户离房
MsgTypeRoomHeartbeat = 154 // 房间心跳
)
const (
......
......@@ -241,6 +241,18 @@ 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 {
logger.Warnf("Unknown message type %d", msgType)
}
......@@ -401,6 +413,54 @@ 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 homePage(w http.ResponseWriter, r *http.Request) {
rsp := "Home Page"
......@@ -568,6 +628,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
......
......@@ -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
......@@ -411,4 +411,19 @@ 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;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment