SignalRSetup.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证。
  2. //
  3. // 必须在法律法规允许的范围内正确使用,严禁将其用于非法、欺诈、恶意或侵犯他人合法权益的目的。
  4. using Furion.Logging.Extensions;
  5. using Microsoft.AspNetCore.DataProtection;
  6. using Newtonsoft.Json;
  7. using StackExchange.Redis;
  8. namespace Admin.NET.Core;
  9. public static class SignalRSetup
  10. {
  11. /// <summary>
  12. /// 即时消息SignalR注册
  13. /// </summary>
  14. /// <param name="services"></param>
  15. /// <param name="SetNewtonsoftJsonSetting"></param>
  16. public static void AddSignalR(this IServiceCollection services, Action<JsonSerializerSettings> SetNewtonsoftJsonSetting)
  17. {
  18. var signalRBuilder = services.AddSignalR(options =>
  19. {
  20. options.EnableDetailedErrors = true;
  21. options.ClientTimeoutInterval = TimeSpan.FromMinutes(2);
  22. options.KeepAliveInterval = TimeSpan.FromMinutes(1);
  23. options.MaximumReceiveMessageSize = 1024 * 1024 * 10; // 数据包大小10M,默认最大为32K
  24. }).AddNewtonsoftJsonProtocol(options => SetNewtonsoftJsonSetting(options.PayloadSerializerSettings));
  25. // 若未启用Redis缓存,直接返回
  26. var cacheOptions = App.GetConfig<CacheOptions>("Cache", true);
  27. if (cacheOptions.CacheType != CacheTypeEnum.Redis.ToString())
  28. return;
  29. // 若已开启集群配置,则把SignalR配置为支持集群模式
  30. var clusterOpt = App.GetConfig<ClusterOptions>("Cluster", true);
  31. if (!clusterOpt.Enabled)
  32. return;
  33. var redisOptions = clusterOpt.SentinelConfig;
  34. ConnectionMultiplexer connection1;
  35. if (clusterOpt.IsSentinel) // 哨兵模式
  36. {
  37. var redisConfig = new ConfigurationOptions
  38. {
  39. AbortOnConnectFail = false,
  40. ServiceName = redisOptions.ServiceName,
  41. AllowAdmin = true,
  42. DefaultDatabase = redisOptions.DefaultDb,
  43. Password = redisOptions.Password
  44. };
  45. redisOptions.EndPoints.ForEach(u => redisConfig.EndPoints.Add(u));
  46. connection1 = ConnectionMultiplexer.Connect(redisConfig);
  47. }
  48. else
  49. {
  50. connection1 = ConnectionMultiplexer.Connect(clusterOpt.SignalR.RedisConfiguration);
  51. }
  52. // 密钥存储(数据保护)
  53. services.AddDataProtection().PersistKeysToStackExchangeRedis(connection1, clusterOpt.DataProtecteKey);
  54. signalRBuilder.AddStackExchangeRedis(options =>
  55. {
  56. // 此处设置的ChannelPrefix并不会生效,如果两个不同的项目,且[程序集名+类名]一样,使用同一个redis服务,请注意修改 Hub/OnlineUserHub 的类名。
  57. // 原因请参考下边链接:
  58. // https://github.com/dotnet/aspnetcore/blob/f9121bc3e976ec40a959818451d126d5126ce868/src/SignalR/server/StackExchangeRedis/src/RedisHubLifetimeManager.cs#L74
  59. // https://github.com/dotnet/aspnetcore/blob/f9121bc3e976ec40a959818451d126d5126ce868/src/SignalR/server/StackExchangeRedis/src/Internal/RedisChannels.cs#L33
  60. options.Configuration.ChannelPrefix = new RedisChannel(clusterOpt.SignalR.ChannelPrefix, RedisChannel.PatternMode.Auto);
  61. options.ConnectionFactory = async writer =>
  62. {
  63. ConnectionMultiplexer connection;
  64. if (clusterOpt.IsSentinel)
  65. {
  66. var config = new ConfigurationOptions
  67. {
  68. AbortOnConnectFail = false,
  69. ServiceName = redisOptions.ServiceName,
  70. AllowAdmin = true,
  71. DefaultDatabase = redisOptions.DefaultDb,
  72. Password = redisOptions.Password
  73. };
  74. redisOptions.EndPoints.ForEach(u => config.EndPoints.Add(u));
  75. connection = await ConnectionMultiplexer.ConnectAsync(config, writer);
  76. }
  77. else
  78. {
  79. connection = await ConnectionMultiplexer.ConnectAsync(clusterOpt.SignalR.RedisConfiguration);
  80. }
  81. connection.ConnectionFailed += (_, e) =>
  82. {
  83. "连接 Redis 失败".LogError();
  84. };
  85. if (!connection.IsConnected)
  86. {
  87. "无法连接 Redis".LogError();
  88. }
  89. return connection;
  90. };
  91. });
  92. }
  93. }