2025-02-17 22:12:35 -05:00

41 lines
1.4 KiB
C#

using Microsoft.AspNetCore.RateLimiting;
using Microsoft.AspNetCore.SignalR;
using QuestShare.Server.Managers;
using Microsoft.AspNetCore.Http.Features;
using QuestShare.Server.Models;
namespace QuestShare.Server.Hubs
{
public partial class ShareHub : Hub
{
public override Task OnConnectedAsync()
{
Log.Information($"Client connected: {Context.ConnectionId}");
if (BanManager.IsBanned(Context))
{
Log.Warning($"Client {Context.ConnectionId} is banned.");
Context.Abort();
}
Clients.Caller.SendAsync(nameof(Authorize.AuthBroadcast), new Authorize.AuthBroadcast
{
});
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception? exception)
{
Log.Information($"Client {Context.ConnectionId} disconnected.");
if (exception != null)
{
Log.Error(exception, "Exception occurred on disconnect.");
}
if (Context.Items["Token"] != null)
{
using var db = new QuestShareContext();
var client = db.Clients.FirstOrDefault(c => c.Token == Context.Items["Token"]!.ToString());
if (client != null) db.Clients.Remove(client);
}
return base.OnDisconnectedAsync(exception);
}
}
}