using System.Collections.Concurrent; namespace HardwareWeb.Services { /// /// 记录当前订阅视频帧的 SignalR 连接。 /// 广播服务据此判断“是否有观看者”,没有观看者时不编码、不发送画面。 /// public sealed class CameraViewerRegistry { private readonly ConcurrentDictionary _viewers = new(StringComparer.Ordinal); public int Count => _viewers.Count; public bool HasViewers => !_viewers.IsEmpty; public bool Add(string connectionId) => _viewers.TryAdd(connectionId, 0); public bool Remove(string connectionId) => _viewers.TryRemove(connectionId, out _); } }