diff --git a/handler/handler.go b/handler/handler.go index 83e3fb1..cdfc9f0 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -10,11 +10,12 @@ import ( type MultiplayerHandler struct { pb.UnimplementedMultiplayerServiceServer - rooms map[string]map[string]*pb.Player - roomsMapMutex sync.RWMutex - jwtManager *jwt.Manager - mongoDB *mongo.Client - cfg *config.Config + rooms map[string]map[string]*pb.Player + roomsMapMutex sync.RWMutex + currentPlayersMutex sync.RWMutex + jwtManager *jwt.Manager + mongoDB *mongo.Client + cfg *config.Config } type AuthHandler struct { diff --git a/handler/room.go b/handler/room.go index 64c161e..72be58f 100644 --- a/handler/room.go +++ b/handler/room.go @@ -79,9 +79,11 @@ func (h *MultiplayerHandler) RoomStream(stream pb.MultiplayerService_RoomStreamS currentPlayers := make([]*pb.Player, 0, len(h.rooms[request.GetRoomId()])) if len(h.rooms[request.GetRoomId()]) > 2 { + h.currentPlayersMutex.Lock() for _, player := range h.rooms[request.GetRoomId()] { currentPlayers = append(currentPlayers, player) } + h.currentPlayersMutex.Unlock() } err = stream.Send(&pb.RoomStreamResponse{Players: currentPlayers})