initial commit
This commit is contained in:
29
Lab505.Script/RealtimeWriter.cs
Normal file
29
Lab505.Script/RealtimeWriter.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user