BotSharp/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs

40 lines
1.3 KiB
C#
Raw Normal View History

2025-07-28 22:41:28 +00:00
using Microsoft.AspNetCore.SignalR;
using System.Runtime.CompilerServices;
namespace BotSharp.Plugin.ChatHub.Helpers;
2025-07-29 15:32:55 +00:00
internal class EventEmitter
2025-07-28 22:41:28 +00:00
{
2025-07-29 15:32:55 +00:00
internal static async Task SendChatEvent<T>(
2025-07-28 22:41:28 +00:00
IServiceProvider services,
ILogger logger,
string @event,
string conversationId,
string userId,
T data,
string callerClass = "",
[CallerMemberName] string callerMethod = "",
LogLevel logLevel = LogLevel.Warning)
{
try
{
var settings = services.GetRequiredService<ChatHubSettings>();
var chatHub = services.GetRequiredService<IHubContext<SignalRHub>>();
2025-07-29 15:32:55 +00:00
switch (settings.EventDispatchBy)
2025-07-28 22:41:28 +00:00
{
2025-07-29 15:32:55 +00:00
case EventDispatchType.Group when !string.IsNullOrEmpty(conversationId):
await chatHub.Clients.Group(conversationId).SendAsync(@event, data);
break;
case EventDispatchType.User when !string.IsNullOrEmpty(userId):
await chatHub.Clients.User(userId).SendAsync(@event, data);
break;
2025-07-28 22:41:28 +00:00
}
}
catch (Exception ex)
{
logger.Log(logLevel, ex, $"Failed to send event '{@event}' in ({callerClass}-{callerMethod}) (conversation id: {conversationId})");
}
}
}