118 lines
4.4 KiB
C#
118 lines
4.4 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace gehGassi.Web.Hubs
|
|
{
|
|
/// <summary>
|
|
/// Hub der App-Nachrichten ermöglicht
|
|
/// </summary>
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
public class AppHub : Hub, IAppHub
|
|
{
|
|
private readonly IHubContext<AppHub> _hubContext;
|
|
private readonly IAppHubUsers _appHubUsers;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
public AppHub(IHubContext<AppHub> hubContext, IAppHubUsers appHubUsers) : base()
|
|
{
|
|
_hubContext = hubContext;
|
|
_appHubUsers = appHubUsers;
|
|
}
|
|
|
|
#region Benutzer
|
|
|
|
/// <summary>
|
|
/// Called when a new connection is established with the hub.
|
|
/// </summary>
|
|
/// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous connect.</returns>
|
|
public override Task OnConnectedAsync()
|
|
{
|
|
if (Context.User?.Identity != null)
|
|
{
|
|
var userName = Context.User.Identity.Name;
|
|
_appHubUsers.ConnectedUsers.TryGetValue(userName, out var existingUserConnectionIds);
|
|
existingUserConnectionIds ??= new List<string>();
|
|
existingUserConnectionIds.Add(Context.ConnectionId);
|
|
_appHubUsers.ConnectedUsers.TryAdd(userName, existingUserConnectionIds);
|
|
}
|
|
|
|
return base.OnConnectedAsync();
|
|
}
|
|
|
|
/// <summary>Called when a connection with the hub is terminated.</summary>
|
|
/// <returns>A <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous disconnect.</returns>
|
|
public override Task OnDisconnectedAsync(Exception? exception)
|
|
{
|
|
if (Context.User?.Identity != null)
|
|
{
|
|
var userName = Context.User.Identity.Name;
|
|
_appHubUsers.ConnectedUsers.TryGetValue(userName, out var existingUserConnectionIds);
|
|
existingUserConnectionIds?.Remove(Context.ConnectionId);
|
|
if (existingUserConnectionIds != null && existingUserConnectionIds.Count == 0)
|
|
{
|
|
_appHubUsers.ConnectedUsers.TryRemove(userName, out var garbage);
|
|
}
|
|
}
|
|
|
|
return base.OnDisconnectedAsync(exception);
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Buchen der Events eines App-Users
|
|
/// </summary>
|
|
/// <param name="appUserId">ID des App-Users</param>
|
|
/// <returns>Task</returns>
|
|
public async Task JoinAppUser(string appUserId)
|
|
{
|
|
var group = $"appuser_{appUserId}";
|
|
await _hubContext.Groups.AddToGroupAsync(Context.ConnectionId, group);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Abbsestellen der Events eines App-Users
|
|
/// </summary>
|
|
/// <param name="appUserId">ID des App-Users</param>
|
|
/// <returns>Task</returns>
|
|
public async Task LeaveAppUser(string appUserId)
|
|
{
|
|
var group = $"appuser_{appUserId}";
|
|
await _hubContext.Groups.RemoveFromGroupAsync(Context.ConnectionId, group);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Buchen der Events einer Konversation für einen Benutzer
|
|
/// </summary>
|
|
/// <param name="conversationId">Id der Konversation</param>
|
|
/// <param name="appUserId">ID des App-Users</param>
|
|
/// <returns>Task</returns>
|
|
public async Task JoinConversation(string conversationId, string appUserId)
|
|
{
|
|
var group = $"conversation_{conversationId}_{appUserId}";
|
|
await _hubContext.Groups.AddToGroupAsync(Context.ConnectionId, group);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Abbsestellen der Events einer Konversation
|
|
/// </summary>
|
|
/// <param name="conversationId">Id der Konversation</param>
|
|
/// <param name="appUserId">ID des App-Users</param>
|
|
/// <returns>Task</returns>
|
|
public async Task LeaveConversation(string conversationId, string appUserId)
|
|
{
|
|
var group = $"conversation_{conversationId}_{appUserId}";
|
|
await _hubContext.Groups.RemoveFromGroupAsync(Context.ConnectionId, group);
|
|
}
|
|
|
|
|
|
}
|
|
}
|