first commit -push
This commit is contained in:
41
RobotNet.RobotManager/HubClients/MapHubClient.cs
Normal file
41
RobotNet.RobotManager/HubClients/MapHubClient.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using OpenIddict.Client;
|
||||
using RobotNet.MapShares.Dtos;
|
||||
using RobotNet.Shares;
|
||||
|
||||
namespace RobotNet.RobotManager.HubClients;
|
||||
|
||||
public class MapHubClient(IHttpClientFactory HttpFactory, OpenIddictClientService openIddictClient)
|
||||
: OpenIddictHubClient(new Uri($"{HttpFactory.CreateClient("MapManagerAPI").BaseAddress}hubs/map/data").ToString(), openIddictClient, ["robotnet-map-api"])
|
||||
{
|
||||
public event Action<Guid>? MapUpdated;
|
||||
private IDisposable? disMapUpdated;
|
||||
|
||||
|
||||
new public async Task StartAsync()
|
||||
{
|
||||
disMapUpdated = Connection.On<Guid>("MapUpdated", mapId => MapUpdated?.Invoke(mapId));
|
||||
|
||||
await base.StartAsync();
|
||||
}
|
||||
|
||||
new public async Task StopAsync()
|
||||
{
|
||||
if (disMapUpdated != null)
|
||||
{
|
||||
disMapUpdated.Dispose();
|
||||
disMapUpdated = null;
|
||||
}
|
||||
await base.StopAsync();
|
||||
}
|
||||
|
||||
public async Task<MessageResult<MapDataDto>> GetMapData(Guid mapId)
|
||||
=> IsConnected ? await Connection.InvokeAsync<MessageResult<MapDataDto>>(nameof(GetMapData), mapId) : new(false, "Kết nối thất bại");
|
||||
|
||||
public async Task<MessageResult<ElementDto[]>> GetElementsState(Guid mapId)
|
||||
=> IsConnected ? await Connection.InvokeAsync<MessageResult<ElementDto[]>>(nameof(GetElementsState), mapId) : new(false, "Kết nối thất bại");
|
||||
public async Task<MessageResult<MapInfoDto>> GetMapInfoByName(string name)
|
||||
=> IsConnected ? await Connection.InvokeAsync<MessageResult<MapInfoDto>>(nameof(GetMapInfoByName), name) : new(false, "Kết nối thất bại");
|
||||
public async Task<MessageResult<MapInfoDto>> GetMapInfoById(Guid mapId)
|
||||
=> IsConnected ? await Connection.InvokeAsync<MessageResult<MapInfoDto>>(nameof(GetMapInfoById), mapId) : new(false, "Kết nối thất bại");
|
||||
}
|
||||
23
RobotNet.RobotManager/HubClients/OpenIddictHubClient.cs
Normal file
23
RobotNet.RobotManager/HubClients/OpenIddictHubClient.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using OpenIddict.Client;
|
||||
using RobotNet.Clients;
|
||||
|
||||
namespace RobotNet.RobotManager.HubClients;
|
||||
|
||||
public class OpenIddictHubClient(string url, OpenIddictClientService openIddictClient, string[] scopes) : HubClient(new Uri(url),
|
||||
async () =>
|
||||
{
|
||||
var result = await openIddictClient.AuthenticateWithClientCredentialsAsync(new()
|
||||
{
|
||||
Scopes = [.. scopes],
|
||||
});
|
||||
if (result == null || result.AccessToken == null || result.AccessTokenExpirationDate == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return result.AccessToken;
|
||||
}
|
||||
})
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user