Files
BeReadyBackend/BeReadyBackend/Hubs/GroupHub.cs
T

24 lines
712 B
C#

using BeReadyBackend.DTO.Messages;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Primitives;
namespace BeReadyBackend.Hubs;
public class GroupHub : Hub
{
public async Task SendMessageToGroup(int groupId, CreateMessageDto message)
{
await Clients.Group($"group-{groupId}").SendAsync("ReceiveMessage", message);
}
public override async Task OnConnectedAsync()
{
HttpContext? httpContext = Context.GetHttpContext();
if (httpContext!.Request.Query.TryGetValue("groupId", out StringValues groupId))
{
await Groups.AddToGroupAsync(Context.ConnectionId, $"group-{groupId}");
}
await base.OnConnectedAsync();
}
}