| | | 1 | | using Microsoft.AspNetCore.SignalR; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.SignalR; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Default SignalR hub for Kestrun providing real-time communication capabilities. |
| | | 7 | | /// Clients can connect to this hub to receive log messages, events, and other real-time updates. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Initializes a new instance of the <see cref="KestrunHub"/> class. |
| | | 11 | | /// </remarks> |
| | | 12 | | /// <param name="logger">The Serilog logger instance.</param> |
| | | 13 | | /// <param name="tracker">The connection tracker for counting connected clients.</param> |
| | 1 | 14 | | public class KestrunHub(Serilog.ILogger logger, IConnectionTracker tracker) : Hub |
| | | 15 | | { |
| | 1 | 16 | | private readonly Serilog.ILogger _logger = logger; |
| | 1 | 17 | | private readonly IConnectionTracker _tracker = tracker; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Called when a client connects to the hub. |
| | | 21 | | /// </summary> |
| | | 22 | | public override async Task OnConnectedAsync() |
| | | 23 | | { |
| | 0 | 24 | | _logger.Information("SignalR client connected: {ConnectionId}", Context.ConnectionId); |
| | 0 | 25 | | _tracker.OnConnected(Context.ConnectionId); |
| | 0 | 26 | | await base.OnConnectedAsync(); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Called when a client disconnects from the hub. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="exception">The exception that caused the disconnection, if any.</param> |
| | | 33 | | public override async Task OnDisconnectedAsync(Exception? exception) |
| | | 34 | | { |
| | 0 | 35 | | if (exception != null) |
| | | 36 | | { |
| | 0 | 37 | | _logger.Warning(exception, "SignalR client disconnected with error: {ConnectionId}", Context.ConnectionId); |
| | | 38 | | } |
| | | 39 | | else |
| | | 40 | | { |
| | 0 | 41 | | _logger.Information("SignalR client disconnected: {ConnectionId}", Context.ConnectionId); |
| | | 42 | | } |
| | 0 | 43 | | _tracker.OnDisconnected(Context.ConnectionId); |
| | 0 | 44 | | await base.OnDisconnectedAsync(exception); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Allows a client to join a specific group. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="groupName">The name of the group to join.</param> |
| | | 51 | | public async Task JoinGroup(string groupName) |
| | | 52 | | { |
| | 0 | 53 | | await Groups.AddToGroupAsync(Context.ConnectionId, groupName); |
| | 0 | 54 | | _logger.Information("Client {ConnectionId} joined group {GroupName}", Context.ConnectionId, groupName); |
| | | 55 | | // Notify caller so UI can update membership state |
| | 0 | 56 | | await Clients.Caller.SendAsync("GroupJoined", groupName); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Allows a client to leave a specific group. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="groupName">The name of the group to leave.</param> |
| | | 63 | | public async Task LeaveGroup(string groupName) |
| | | 64 | | { |
| | 0 | 65 | | await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName); |
| | 0 | 66 | | _logger.Information("Client {ConnectionId} left group {GroupName}", Context.ConnectionId, groupName); |
| | | 67 | | // Notify caller so UI can update membership state |
| | 0 | 68 | | await Clients.Caller.SendAsync("GroupLeft", groupName); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Echoes a message back to the caller (useful for testing). |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="message">The message to echo.</param> |
| | 0 | 75 | | public async Task Echo(string message) => await Clients.Caller.SendAsync("ReceiveEcho", message); |
| | | 76 | | } |