using System.Text; using Microsoft.AspNetCore.SignalR; namespace Lab505.Script; public class RealtimeWriter : TextWriter { private readonly IHubContext _hub; private readonly string? _userGroup; // "user-{uid}" — routes to all tabs of one user public override Encoding Encoding => Encoding.UTF8; public RealtimeWriter(IHubContext hub, string? userId = null) { _hub = hub; _userGroup = userId is not null ? $"user-{userId}" : null; } public override void WriteLine(string? value) => Send(value ?? ""); public override void Write(string? value) => Send(value ?? ""); private void Send(string value) { var target = _userGroup is not null ? _hub.Clients.Group(_userGroup) : _hub.Clients.All; // fallback only (should not happen in practice) target.SendAsync("scriptLog", value); } }