♻️ Refactor the way to handle websocket

This commit is contained in:
2025-09-21 23:07:20 +08:00
parent e3657386cd
commit 204640a759
13 changed files with 196 additions and 219 deletions

View File

@@ -1,5 +1,6 @@
using System.Net.WebSockets;
using DysonNetwork.Shared.Proto;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
namespace DysonNetwork.Ring.Connection;

View File

@@ -3,11 +3,15 @@ using DysonNetwork.Shared.Proto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
namespace DysonNetwork.Ring.Connection;
[ApiController]
public class WebSocketController(WebSocketService ws, ILogger<WebSocketContext> logger) : ControllerBase
public class WebSocketController(
WebSocketService ws,
ILogger<WebSocketContext> logger
) : ControllerBase
{
[Route("/ws")]
[Authorize]
@@ -23,7 +27,7 @@ public class WebSocketController(WebSocketService ws, ILogger<WebSocketContext>
return;
}
var accountId = currentUser.Id!;
var accountId = Guid.Parse(currentUser.Id!);
var deviceId = currentSession.Challenge?.DeviceId ?? Guid.NewGuid().ToString();
if (string.IsNullOrEmpty(deviceId))
@@ -89,7 +93,7 @@ public class WebSocketController(WebSocketService ws, ILogger<WebSocketContext>
CancellationToken cancellationToken
)
{
var connectionKey = (AccountId: currentUser.Id, DeviceId: deviceId);
var connectionKey = (AccountId: Guid.Parse(currentUser.Id), DeviceId: deviceId);
var buffer = new byte[1024 * 4];
try

View File

@@ -1,97 +0,0 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using DysonNetwork.Shared.Proto;
using NodaTime;
using NodaTime.Serialization.SystemTextJson;
namespace DysonNetwork.Ring.Connection;
public class WebSocketPacket
{
public string Type { get; set; } = null!;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? Data { get; set; } = null!;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Endpoint { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ErrorMessage { get; set; }
/// <summary>
/// Creates a WebSocketPacket from raw WebSocket message bytes
/// </summary>
/// <param name="bytes">Raw WebSocket message bytes</param>
/// <returns>Deserialized WebSocketPacket</returns>
public static WebSocketPacket FromBytes(byte[] bytes)
{
var json = System.Text.Encoding.UTF8.GetString(bytes);
var jsonOpts = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower,
};
return JsonSerializer.Deserialize<WebSocketPacket>(json, jsonOpts) ??
throw new JsonException("Failed to deserialize WebSocketPacket");
}
/// <summary>
/// Deserializes the Data property to the specified type T
/// </summary>
/// <typeparam name="T">Target type to deserialize to</typeparam>
/// <returns>Deserialized data of type T</returns>
public T? GetData<T>()
{
if (Data is T typedData)
return typedData;
var jsonOpts = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower,
};
return JsonSerializer.Deserialize<T>(
JsonSerializer.Serialize(Data, jsonOpts),
jsonOpts
);
}
/// <summary>
/// Serializes this WebSocketPacket to a byte array for sending over WebSocket
/// </summary>
/// <returns>Byte array representation of the packet</returns>
public byte[] ToBytes()
{
var jsonOpts = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower,
}.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
var json = JsonSerializer.Serialize(this, jsonOpts);
return System.Text.Encoding.UTF8.GetBytes(json);
}
public Shared.Proto.WebSocketPacket ToProtoValue()
{
return new Shared.Proto.WebSocketPacket
{
Type = Type,
Data = GrpcTypeHelper.ConvertObjectToByteString(Data),
ErrorMessage = ErrorMessage
};
}
public static WebSocketPacket FromProtoValue(Shared.Proto.WebSocketPacket packet)
{
return new WebSocketPacket
{
Type = packet.Type,
Data = GrpcTypeHelper.ConvertByteStringToObject<object?>(packet.Data),
ErrorMessage = packet.ErrorMessage
};
}
}

View File

@@ -2,36 +2,38 @@ using System.Collections.Concurrent;
using System.Net.WebSockets;
using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Proto;
using Grpc.Core;
using DysonNetwork.Shared.Stream;
using NATS.Client.Core;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
namespace DysonNetwork.Ring.Connection;
public class WebSocketService
{
private readonly IConfiguration _configuration;
private readonly INatsConnection _nats;
private readonly ILogger<WebSocketService> _logger;
private readonly IDictionary<string, IWebSocketPacketHandler> _handlerMap;
public WebSocketService(
IEnumerable<IWebSocketPacketHandler> handlers,
ILogger<WebSocketService> logger,
IConfiguration configuration
INatsConnection nats
)
{
_logger = logger;
_configuration = configuration;
_handlerMap = handlers.ToDictionary(h => h.PacketType);
_nats = nats;
}
private static readonly ConcurrentDictionary<
(string AccountId, string DeviceId),
(Guid AccountId, string DeviceId),
(WebSocket Socket, CancellationTokenSource Cts)
> ActiveConnections = new();
private static readonly ConcurrentDictionary<string, string> ActiveSubscriptions = new(); // deviceId -> chatRoomId
public bool TryAdd(
(string AccountId, string DeviceId) key,
(Guid AccountId, string DeviceId) key,
WebSocket socket,
CancellationTokenSource cts
)
@@ -42,7 +44,7 @@ public class WebSocketService
return ActiveConnections.TryAdd(key, (socket, cts));
}
public void Disconnect((string AccountId, string DeviceId) key, string? reason = null)
public void Disconnect((Guid AccountId, string DeviceId) key, string? reason = null)
{
if (!ActiveConnections.TryGetValue(key, out var data)) return;
try
@@ -63,19 +65,19 @@ public class WebSocketService
ActiveConnections.TryRemove(key, out _);
}
public bool GetDeviceIsConnected(string deviceId)
public static bool GetDeviceIsConnected(string deviceId)
{
return ActiveConnections.Any(c => c.Key.DeviceId == deviceId);
}
public bool GetAccountIsConnected(string accountId)
public static bool GetAccountIsConnected(Guid accountId)
{
return ActiveConnections.Any(c => c.Key.AccountId == accountId);
}
public void SendPacketToAccount(string userId, WebSocketPacket packet)
public static void SendPacketToAccount(Guid accountId, WebSocketPacket packet)
{
var connections = ActiveConnections.Where(c => c.Key.AccountId == userId);
var connections = ActiveConnections.Where(c => c.Key.AccountId == accountId);
var packetBytes = packet.ToBytes();
var segment = new ArraySegment<byte>(packetBytes);
@@ -139,28 +141,16 @@ public class WebSocketService
try
{
var endpoint = packet.Endpoint.Replace("DysonNetwork.", "").ToLower();
var serviceUrl = "https://_grpc." + endpoint;
var callInvoker = GrpcClientHelper.CreateCallInvoker(serviceUrl);
var client = new RingHandlerService.RingHandlerServiceClient(callInvoker);
try
await _nats.PublishAsync(WebSocketPacketEvent.SubjectPrefix + endpoint, new WebSocketPacketEvent
{
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} (${endpoint})");
}
AccountId = Guid.Parse(currentUser.Id),
DeviceId = deviceId,
PacketBytes = packet.ToBytes()
});
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error forwarding packet to endpoint: {packet.Endpoint}");
_logger.LogError(ex, "Error forwarding packet to endpoint: {Endpoint}", packet.Endpoint);
}
}
@@ -175,4 +165,4 @@ public class WebSocketService
CancellationToken.None
);
}
}
}

View File

@@ -5,14 +5,13 @@ using DysonNetwork.Ring.Services;
using DysonNetwork.Shared.Proto;
using Microsoft.EntityFrameworkCore;
using NodaTime;
using WebSocketPacket = DysonNetwork.Ring.Connection.WebSocketPacket;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
namespace DysonNetwork.Ring.Notification;
public class PushService
{
private readonly AppDatabase _db;
private readonly WebSocketService _ws;
private readonly QueueService _queueService;
private readonly ILogger<PushService> _logger;
private readonly FirebaseSender? _fcm;
@@ -22,7 +21,6 @@ public class PushService
public PushService(
IConfiguration config,
AppDatabase db,
WebSocketService ws,
QueueService queueService,
IHttpClientFactory httpFactory,
ILogger<PushService> logger
@@ -53,7 +51,6 @@ public class PushService
}
_db = db;
_ws = ws;
_queueService = queueService;
_logger = logger;
}
@@ -73,9 +70,9 @@ public class PushService
)
{
var now = SystemClock.Instance.GetCurrentInstant();
var accountId = Guid.Parse(account.Id!);
var accountId = Guid.Parse(account.Id);
// Check for existing subscription with same device ID or token
// Check for existing subscription with the same device ID or token
var existingSubscription = await _db.PushSubscriptions
.Where(s => s.AccountId == accountId)
.Where(s => s.DeviceId == deviceId || s.DeviceToken == deviceToken)
@@ -125,7 +122,7 @@ public class PushService
if (actionUri is not null) meta["action_uri"] = actionUri;
var accountId = Guid.Parse(account.Id!);
var accountId = account.Id;
var notification = new Notification
{
Topic = topic,
@@ -133,7 +130,7 @@ public class PushService
Subtitle = subtitle,
Content = content,
Meta = meta,
AccountId = accountId,
AccountId = Guid.Parse(accountId),
};
if (save)
@@ -143,12 +140,12 @@ public class PushService
}
if (!isSilent)
_ = _queueService.EnqueuePushNotification(notification, accountId, save);
_ = _queueService.EnqueuePushNotification(notification, Guid.Parse(accountId), save);
}
public async Task DeliverPushNotification(Notification notification, CancellationToken cancellationToken = default)
{
_ws.SendPacketToAccount(notification.AccountId.ToString(), new WebSocketPacket()
WebSocketService.SendPacketToAccount(notification.AccountId, new WebSocketPacket()
{
Type = "notifications.new",
Data = notification,
@@ -251,8 +248,8 @@ public class PushService
// WS first
foreach (var account in accounts)
{
notification.AccountId = account; // keep original behavior
_ws.SendPacketToAccount(account.ToString(), new Connection.WebSocketPacket
notification.AccountId = account;
WebSocketService.SendPacketToAccount(account, new WebSocketPacket
{
Type = "notifications.new",
Data = notification

View File

@@ -1,10 +1,8 @@
using DysonNetwork.Ring.Connection;
using DysonNetwork.Ring.Email;
using DysonNetwork.Ring.Notification;
using DysonNetwork.Shared.Proto;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using System.Text.Json;
namespace DysonNetwork.Ring.Services;
@@ -27,30 +25,28 @@ public class RingServiceGrpc(
public override Task<Empty> PushWebSocketPacket(PushWebSocketPacketRequest request, ServerCallContext context)
{
var packet = new Connection.WebSocketPacket
var packet = new Shared.Data.WebSocketPacket
{
Type = request.Packet.Type,
Data = GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Packet.Data),
ErrorMessage = request.Packet.ErrorMessage
};
websocket.SendPacketToAccount(request.UserId, packet);
WebSocketService.SendPacketToAccount(Guid.Parse(request.UserId), packet);
return Task.FromResult(new Empty());
}
public override Task<Empty> PushWebSocketPacketToUsers(PushWebSocketPacketToUsersRequest request,
ServerCallContext context)
{
var packet = new Connection.WebSocketPacket
var packet = new Shared.Data.WebSocketPacket
{
Type = request.Packet.Type,
Data = GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Packet.Data),
ErrorMessage = request.Packet.ErrorMessage
};
foreach (var userId in request.UserIds)
{
websocket.SendPacketToAccount(userId, packet);
}
foreach (var accountId in request.UserIds)
WebSocketService.SendPacketToAccount(Guid.Parse(accountId), packet);
return Task.FromResult(new Empty());
}
@@ -58,7 +54,7 @@ public class RingServiceGrpc(
public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDeviceRequest request,
ServerCallContext context)
{
var packet = new Connection.WebSocketPacket
var packet = new Shared.Data.WebSocketPacket
{
Type = request.Packet.Type,
Data = GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Packet.Data),
@@ -71,7 +67,7 @@ public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDev
public override Task<Empty> PushWebSocketPacketToDevices(PushWebSocketPacketToDevicesRequest request,
ServerCallContext context)
{
var packet = new Connection.WebSocketPacket
var packet = new Shared.Data.WebSocketPacket
{
Type = request.Packet.Type,
Data = GrpcTypeHelper.ConvertByteStringToObject<Dictionary<string, object?>>(request.Packet.Data),
@@ -161,8 +157,8 @@ public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDev
var isConnected = request.IdCase switch
{
GetWebsocketConnectionStatusRequest.IdOneofCase.DeviceId =>
websocket.GetDeviceIsConnected(request.DeviceId),
GetWebsocketConnectionStatusRequest.IdOneofCase.UserId => websocket.GetAccountIsConnected(request.UserId),
WebSocketService.GetDeviceIsConnected(request.DeviceId),
GetWebsocketConnectionStatusRequest.IdOneofCase.UserId => WebSocketService.GetAccountIsConnected(Guid.Parse(request.UserId)),
_ => false
};