// Admin.NET 项目的版æƒã€å•†æ ‡ã€ä¸“利和其他相关æƒåˆ©å‡å—ç›¸åº”æ³•å¾‹æ³•è§„çš„ä¿æŠ¤ã€‚ä½¿ç”¨æœ¬é¡¹ç›®åº”éµå®ˆç›¸å…³æ³•律法规和许å¯è¯çš„è¦æ±‚。 // // 本项目主è¦éµå¾ª MIT 许å¯è¯å’Œ Apache 许å¯è¯ï¼ˆç‰ˆæœ¬ 2.0)进行分å‘和使用。许å¯è¯ä½äºŽæºä»£ç æ ‘æ ¹ç›®å½•ä¸çš„ LICENSE-MIT å’Œ LICENSE-APACHE 文件。 // // ä¸å¾—利用本项目从事å±å®³å›½å®¶å®‰å…¨ã€æ‰°ä¹±ç¤¾ä¼šç§©åºã€ä¾µçŠ¯ä»–äººåˆæ³•æƒç›Šç‰æ³•å¾‹æ³•è§„ç¦æ¢çš„æ´»åЍï¼ä»»ä½•基于本项目二次开å‘è€Œäº§ç”Ÿçš„ä¸€åˆ‡æ³•å¾‹çº çº·å’Œè´£ä»»ï¼Œæˆ‘ä»¬ä¸æ‰¿æ‹…ä»»ä½•è´£ä»»ï¼ using Furion.Logging.Extensions; using Microsoft.AspNetCore.DataProtection; using Newtonsoft.Json; using StackExchange.Redis; namespace Admin.NET.Core; public static class SignalRSetup { /// <summary> /// 峿—¶æ¶ˆæ¯SignalR注册 /// </summary> /// <param name="services"></param> /// <param name="SetNewtonsoftJsonSetting"></param> public static void AddSignalR(this IServiceCollection services, Action<JsonSerializerSettings> SetNewtonsoftJsonSetting) { var signalRBuilder = services.AddSignalR(options => { options.EnableDetailedErrors = true; options.ClientTimeoutInterval = TimeSpan.FromMinutes(2); options.KeepAliveInterval = TimeSpan.FromMinutes(1); options.MaximumReceiveMessageSize = 1024 * 1024 * 10; // æ•°æ®åŒ…大å°10M,默认最大为32K }).AddNewtonsoftJsonProtocol(options => SetNewtonsoftJsonSetting(options.PayloadSerializerSettings)); // 若未å¯ç”¨Redis缓å˜ï¼Œç›´æŽ¥è¿”回 var cacheOptions = App.GetConfig<CacheOptions>("Cache", true); if (cacheOptions.CacheType != CacheTypeEnum.Redis.ToString()) return; // 若已开å¯é›†ç¾¤é…置,则把SignalRé…置为支æŒé›†ç¾¤æ¨¡å¼ var clusterOpt = App.GetConfig<ClusterOptions>("Cluster", true); if (!clusterOpt.Enabled) return; var redisOptions = clusterOpt.SentinelConfig; ConnectionMultiplexer connection1; if (clusterOpt.IsSentinel) // å“¨å…µæ¨¡å¼ { var redisConfig = new ConfigurationOptions { AbortOnConnectFail = false, ServiceName = redisOptions.ServiceName, AllowAdmin = true, DefaultDatabase = redisOptions.DefaultDb, Password = redisOptions.Password }; redisOptions.EndPoints.ForEach(u => redisConfig.EndPoints.Add(u)); connection1 = ConnectionMultiplexer.Connect(redisConfig); } else { connection1 = ConnectionMultiplexer.Connect(clusterOpt.SignalR.RedisConfiguration); } // 密钥å˜å‚¨ï¼ˆæ•°æ®ä¿æŠ¤ï¼‰ services.AddDataProtection().PersistKeysToStackExchangeRedis(connection1, clusterOpt.DataProtecteKey); signalRBuilder.AddStackExchangeRedis(options => { // æ¤å¤„设置的ChannelPrefixå¹¶ä¸ä¼šç”Ÿæ•ˆï¼Œå¦‚果两个ä¸åŒçš„项目,且[程åºé›†å+ç±»å]ä¸€æ ·ï¼Œä½¿ç”¨åŒä¸€ä¸ªredisæœåŠ¡ï¼Œè¯·æ³¨æ„修改 Hub/OnlineUserHub 的类å。 // åŽŸå› è¯·å‚考下边链接: // https://github.com/dotnet/aspnetcore/blob/f9121bc3e976ec40a959818451d126d5126ce868/src/SignalR/server/StackExchangeRedis/src/RedisHubLifetimeManager.cs#L74 // https://github.com/dotnet/aspnetcore/blob/f9121bc3e976ec40a959818451d126d5126ce868/src/SignalR/server/StackExchangeRedis/src/Internal/RedisChannels.cs#L33 options.Configuration.ChannelPrefix = new RedisChannel(clusterOpt.SignalR.ChannelPrefix, RedisChannel.PatternMode.Auto); options.ConnectionFactory = async writer => { ConnectionMultiplexer connection; if (clusterOpt.IsSentinel) { var config = new ConfigurationOptions { AbortOnConnectFail = false, ServiceName = redisOptions.ServiceName, AllowAdmin = true, DefaultDatabase = redisOptions.DefaultDb, Password = redisOptions.Password }; redisOptions.EndPoints.ForEach(u => config.EndPoints.Add(u)); connection = await ConnectionMultiplexer.ConnectAsync(config, writer); } else { connection = await ConnectionMultiplexer.ConnectAsync(clusterOpt.SignalR.RedisConfiguration); } connection.ConnectionFailed += (_, e) => { "连接 Redis 失败".LogError(); }; if (!connection.IsConnected) { "æ— æ³•è¿žæŽ¥ Redis".LogError(); } return connection; }; }); } }