From 89a6028deda4e625d22ac84fdd61d346b6b80669 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 25 Jul 2023 11:26:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E5=9C=A8?= =?UTF-8?q?=E6=88=BF=E9=97=B4=E7=9A=84=E6=9C=80=E5=90=8E=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E5=BF=83=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 10 ++++++++++ manager/roomManager.go | 11 +++++++++++ protocol/userCenter.proto | 11 +++++++++++ 3 files changed, 32 insertions(+) diff --git a/main.go b/main.go index 8a041f9..53f4294 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 3485960..8e7b51b 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 644f0a4..d827270 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 -- 2.22.0