You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
4.7 KiB
122 lines
4.7 KiB
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using CoreAgent.WebSocketTransport.Interfaces;
|
|
using CoreAgent.WebSocketTransport.Services;
|
|
using CoreAgent.WebSocketTransport.Middleware;
|
|
using CoreAgent.WebSocketTransport.Models;
|
|
|
|
namespace CoreAgent.WebSocketTransport.Extensions
|
|
{
|
|
/// <summary>
|
|
/// WebSocket 传输服务扩展
|
|
/// </summary>
|
|
public static class WebSocketTransportExtensions
|
|
{
|
|
/// <summary>
|
|
/// 添加 WebSocket 传输服务
|
|
/// </summary>
|
|
/// <param name="services">服务集合</param>
|
|
/// <param name="configuration">配置</param>
|
|
/// <param name="configSection">配置节名称</param>
|
|
/// <returns>服务集合</returns>
|
|
public static IServiceCollection AddWebSocketTransport(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
string configSection = "WebSocket")
|
|
{
|
|
if (services == null)
|
|
throw new ArgumentNullException(nameof(services));
|
|
if (configuration == null)
|
|
throw new ArgumentNullException(nameof(configuration));
|
|
|
|
// 注册配置
|
|
services.Configure<WebSocketConfig>(options =>
|
|
{
|
|
configuration.GetSection(configSection).Bind(options);
|
|
});
|
|
|
|
// 注册默认中间件(在核心服务之前)
|
|
RegisterDefaultMiddleware(services);
|
|
|
|
// 注册核心服务
|
|
RegisterCoreServices(services);
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加 WebSocket 中间件
|
|
/// </summary>
|
|
/// <typeparam name="T">中间件类型</typeparam>
|
|
/// <param name="services">服务集合</param>
|
|
/// <returns>服务集合</returns>
|
|
public static IServiceCollection AddWebSocketMiddleware<T>(this IServiceCollection services)
|
|
where T : class, IMessageMiddleware
|
|
{
|
|
if (services == null)
|
|
throw new ArgumentNullException(nameof(services));
|
|
|
|
services.AddTransient<IMessageMiddleware, T>();
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册核心服务
|
|
/// </summary>
|
|
/// <param name="services">服务集合</param>
|
|
private static void RegisterCoreServices(IServiceCollection services)
|
|
{
|
|
// 注册核心组件
|
|
services.AddSingleton<IWebSocketConnection, WebSocketConnection>();
|
|
services.AddSingleton<IMessageSerializer, JsonMessageSerializer>();
|
|
|
|
// 注册消息通道管理器
|
|
services.AddSingleton<IMessageChannelManager>(provider =>
|
|
{
|
|
var logger = provider.GetRequiredService<ILogger<MessageChannelManager>>();
|
|
var config = provider.GetRequiredService<IOptions<WebSocketConfig>>().Value;
|
|
|
|
return new MessageChannelManager(
|
|
logger,
|
|
config.SendChannelCapacity,
|
|
config.ReceiveChannelCapacity,
|
|
config.PriorityChannelCapacity);
|
|
});
|
|
|
|
// 注册 WebSocket 传输
|
|
services.AddSingleton<IWebSocketTransport>(provider =>
|
|
{
|
|
var logger = provider.GetRequiredService<ILogger<CoreAgent.WebSocketTransport.Services.WebSocketTransport>>();
|
|
var connection = provider.GetRequiredService<IWebSocketConnection>();
|
|
var serializer = provider.GetRequiredService<IMessageSerializer>();
|
|
var middlewares = provider.GetServices<IMessageMiddleware>();
|
|
var config = provider.GetRequiredService<IOptions<WebSocketConfig>>().Value;
|
|
var channelManager = provider.GetRequiredService<IMessageChannelManager>();
|
|
|
|
return new CoreAgent.WebSocketTransport.Services.WebSocketTransport(
|
|
logger,
|
|
connection,
|
|
serializer,
|
|
middlewares,
|
|
config,
|
|
channelManager);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册默认中间件
|
|
/// </summary>
|
|
/// <param name="services">服务集合</param>
|
|
private static void RegisterDefaultMiddleware(IServiceCollection services)
|
|
{
|
|
// 注册日志中间件
|
|
services.AddWebSocketMiddleware<LoggingMiddleware>();
|
|
|
|
// 注册缓存中间件(使用正确的注册方式)
|
|
services.AddWebSocketMiddleware<CacheMiddleware>();
|
|
}
|
|
}
|
|
}
|