✨ Websocket handler
This commit is contained in:
@@ -1,16 +1,29 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.WebSockets;
|
||||
using dotnet_etcd.interfaces;
|
||||
using DysonNetwork.Shared.Data;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace DysonNetwork.Pusher.Connection;
|
||||
|
||||
public class WebSocketService
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<WebSocketService> _logger;
|
||||
private readonly IEtcdClient _etcdClient;
|
||||
private readonly IDictionary<string, IWebSocketPacketHandler> _handlerMap;
|
||||
|
||||
public WebSocketService(IEnumerable<IWebSocketPacketHandler> handlers)
|
||||
public WebSocketService(
|
||||
IEnumerable<IWebSocketPacketHandler> handlers,
|
||||
IEtcdClient etcdClient,
|
||||
ILogger<WebSocketService> logger,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
_etcdClient = etcdClient;
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_handlerMap = handlers.ToDictionary(h => h.PacketType);
|
||||
}
|
||||
|
||||
@@ -44,7 +57,7 @@ public class WebSocketService
|
||||
data.Cts.Cancel();
|
||||
ActiveConnections.TryRemove(key, out _);
|
||||
}
|
||||
|
||||
|
||||
public bool GetDeviceIsConnected(string deviceId)
|
||||
{
|
||||
return ActiveConnections.Any(c => c.Key.DeviceId == deviceId);
|
||||
@@ -102,6 +115,56 @@ public class WebSocketService
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet.Endpoint is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get the service URL from etcd for the specified endpoint
|
||||
var serviceKey = $"/services/{packet.Endpoint}";
|
||||
var response = await _etcdClient.GetAsync(serviceKey);
|
||||
|
||||
if (response.Kvs.Count > 0)
|
||||
{
|
||||
var serviceUrl = response.Kvs[0].Value.ToStringUtf8();
|
||||
|
||||
var clientCertPath = _configuration["Service:ClientCert"]!;
|
||||
var clientKeyPath = _configuration["Service:ClientKey"]!;
|
||||
var clientCertPassword = _configuration["Service:CertPassword"];
|
||||
|
||||
var callInvoker =
|
||||
GrpcClientHelper.CreateCallInvoker(
|
||||
serviceUrl,
|
||||
clientCertPath,
|
||||
clientKeyPath,
|
||||
clientCertPassword
|
||||
);
|
||||
var client = new PusherHandlerService.PusherHandlerServiceClient(callInvoker);
|
||||
|
||||
try
|
||||
{
|
||||
await client.ReceiveWebSocketPacketAsync(new ReceiveWebSocketPacketRequest
|
||||
{
|
||||
Account = currentUser,
|
||||
DeviceId = deviceId,
|
||||
Packet = packet.ToProtoValue()
|
||||
});
|
||||
}
|
||||
catch (RpcException ex)
|
||||
{
|
||||
_logger.LogError(ex, $"Error forwarding packet to endpoint: {packet.Endpoint}");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogWarning($"No service registered for endpoint: {packet.Endpoint}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"Error forwarding packet to endpoint: {packet.Endpoint}");
|
||||
}
|
||||
}
|
||||
|
||||
await socket.SendAsync(
|
||||
new ArraySegment<byte>(new WebSocketPacket
|
||||
{
|
||||
|
Reference in New Issue
Block a user