Files
Lab505/Lab505.Script/RealtimeWriter.cs
2026-10-12 15:24:08 +07:00

30 lines
921 B
C#

using System.Text;
using Microsoft.AspNetCore.SignalR;
namespace Lab505.Script;
public class RealtimeWriter : TextWriter
{
private readonly IHubContext<ScriptHub> _hub;
private readonly string? _userGroup; // "user-{uid}" — routes to all tabs of one user
public override Encoding Encoding => Encoding.UTF8;
public RealtimeWriter(IHubContext<ScriptHub> 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);
}
}