diff --git a/main.go b/main.go index 8a041f93b85bb8f6e63d4629d801d96a7e906b58..53f4294c075808e75431bc5a791753c7f0842178 100644 --- a/main.go +++ b/main.go @@ -285,6 +285,16 @@ func (s *server) RoomHeartbeat(ctx context.Context, in *userCenter.RoomHeartbeat }, nil } +func (s *server) GetLastRoomHeartbeat(ctx context.Context, in *userCenter.GetLastRoomHeartbeatMessage) (*userCenter.GetLastRoomHeartbeatMessageResp, error) { + ts, err := roomManager.GetLastRoomUserHeartbeat(in.GetUid(), in.GetGroupId()) + if err != nil { + return nil, err + } + return &userCenter.GetLastRoomHeartbeatMessageResp{ + Timestamp: ts, + }, nil +} + func realBroadcast(addr string, uids []uint64, msg *userCenter.BroadcastMessage) { mylogrus.MyLog.Infof("Broadcasting: Addr %s: users: %v", addr, uids) diff --git a/manager/roomManager.go b/manager/roomManager.go index 3485960b8f4aca3074f41ea8e9751644df9d639b..8e7b51b14858feec665f63bef0e4fd2e8ce86ad0 100644 --- a/manager/roomManager.go +++ b/manager/roomManager.go @@ -54,6 +54,17 @@ func (m *RoomManager) UpdateRoomUser(uid uint64, groupId string) error { return err } +// 获取用户在房间的最后一次心跳 +func (m *RoomManager) GetLastRoomUserHeartbeat(uid uint64, groupId string) (int64, error) { + key := getRoomUserKey(groupId) + tx, err := m.RedisClient.ZScore(m.Ctx, key, fmt.Sprintf("%d", uid)).Result() + if err != nil { + mylogrus.MyLog.Errorf("GetLastRoomUserHeartbeat fail:%v", err) + return 0, err + } + return int64(tx), err +} + func (m *RoomManager) GetAll() *map[string]string { //ctx, _ := context.WithTimeout(m.Ctx, time.Millisecond*500) //result := m.RedisClient.Get(ctx, field) diff --git a/protocol/userCenter.proto b/protocol/userCenter.proto index 644f0a4c685cfbce0d8e9da4144410f6ecf53bc8..d827270d8c6193829bfff50b036d7007417cf15c 100644 --- a/protocol/userCenter.proto +++ b/protocol/userCenter.proto @@ -112,6 +112,16 @@ message RoomHeartbeatMessageRsp { uint32 status = 1; } +// 获取房间内最后一次心跳 +message GetLastRoomHeartbeatMessage { + uint64 uid = 1; + string groupId = 2; +} + +message GetLastRoomHeartbeatMessageResp { + int64 timestamp = 1; +} + service Router { rpc route(RouteMessage) returns (RouteMessageRsp) {} rpc kickUser(KickMessage) returns (KickMessageRsp) {} @@ -126,4 +136,5 @@ service User { rpc enterRoom(EnterRoomMessage) returns (EnterRoomMessageRsp) {} rpc leaveRoom(LeaveRoomMessage) returns (LeaveRoomMessageRsp) {} rpc roomHeartbeat(RoomHeartbeatMessage) returns (RoomHeartbeatMessageRsp) {} + rpc getLastRoomHeartbeat(GetLastRoomHeartbeatMessage) returns (GetLastRoomHeartbeatMessageResp) {} } \ No newline at end of file