| asp.net core 利用 signalR(一)Intro
SignalR 是什么?ASP.NET Core SignalR 是一个开源代码库,它简化了向应用添加实时 Web 功能的过程。 实时 Web 功能使服务器端代码能够即时将内容推送到客户端。 SignalR 的实用对象: 
必要来自服务器的高频率更新的应用。 比方:游戏、交际网络、投票、拍卖、地图和 GPS 应用。仪表板和监视应用。 示例包罗公司仪表板、贩卖状态即时更新或行程警示。协作应用。 协作应用的示例包罗白板应用和团队聚会会议软件。必要关照的应用。 交际网络、电子邮件、聊天、游戏、行程警示以及许多其他应用都利用关照。 SignalR 提供了一个用于创建服务器到客户端远程过程调用(RPC)的 API。 RPC 通过服务器端 .NET Core 代码调用客户端上的 JavaScript 函数。 以下是 ASP.NET Core SignalR 的一些功能: 
自动管理毗连。同时向所有毗连的客户端发送消息。 比方,聊天室。将消息发送到特定的客户端或客户端组。扩展以处置惩罚增长的流量。 传输SignalR 支持几种方法用于处置惩罚实时通讯: 迩来我们在做一个对战的小游戏,类似于之前比较火的答题应用,利用 websocket 来实现客户端和服务器端的通讯,服务器端利用的 SignalR SignR 根本利用服务注册服务设置如下: 复制代码<code>services.AddSignalR(options =>    {        options.HandshakeTimeout = TimeSpan.FromSeconds(3);        options.KeepAliveInterval = TimeSpan.FromSeconds(10);    })    // JSON 序列化设置    .AddJsonProtocol(options =>    {        options.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver();        options.PayloadSerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;        options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;        options.PayloadSerializerSettings.NullValueHandling = NullValueHandling.Ignore;    });</code>
认证方式设置默认的 Token 是从哀求头 Authorization中获取的,而 signalr 哀求服务器端的时候是放在哀求地址的 query stringaccess-token内里的,以是我们要设置从哀求头中获取大概从QueryString里获取,示例设置如下: 复制代码<code>services.AddAuthentication(options =>    {        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;        options.DefaultForbidScheme = JwtBearerDefaults.AuthenticationScheme;    })    .AddIdentityServerAuthentication(options =>    {        options.Authority = Configuration["Authorization:Authority"];        options.RequireHttpsMetadata = false;        options.TokenRetriever = request =>        {            var token = TokenRetrieval.FromAuthorizationHeader()(request);            if (string.IsNullOrWhiteSpace(token))            {                token = TokenRetrieval.FromQueryString()(request);            }            return token;        };    });</code>
Configue 设置复制代码<code>app.UseAuthentication();app.UseSignalR(builder =>{    builder.MapHub<QuizGameHub>("/hubs/quizGame"); // 注册 Hub});app.UseMvc();</code>
自界说 Hub界说 Hub 左券界说一个客户端方法的接口以实现强类型的客户端方法调用,这里客户端调用服务器端的方法也界说了一个接口来束缚,示比方下: 复制代码<code>/// <summary>/// 客户端界说的方法/// </summary>public interface IQuizGameClient{    Task GameQuestionsReceived(QuizQuestion question);    Task MatchSuccess(GameInfo gameInfo);   Task GameAnswerResultReceived(CheckedUserQuizAnswerModel answer);    Task GameOver(GameResult result);}/// <summary>/// 服务器端界说的方法/// </summary>public interface IQuizGameServer{    Task<ServiceResult<IReadOnlyList<QuizGameRuleInfo>>> GetGameRules();    Task AutoMatch(int ruleId);    Task CheckQuestionAnswer(BaseQuizAnswer model, string gameId);}</code>
界说 Hub有了左券之后,我们就可以界说强类型的 Hub 了,示比方下: 复制代码<code>[Authorize(Policy = "bearer")]public partial class QuizGameHub : Hub<IQuizGameClient>, IQuizGameServer{    public Task<ServiceResult<IReadOnlyList<QuizGameRuleInfo>>> GetGameRules()    {        return Task.FromResult(ServiceResult.Success(QuizGameStorage.GameRuleInfos));    }        // ...    public async Task CheckQuestionAnswer(BaseQuizAnswer model, string gameId)    {        // 调用客户端方法        await Clients.User(Context.UserIdentifier)            .GameAnswerResultReceived(checkedResult); // 向指定用户发送消息    }        public async Task AutoMatch(int ruleId)    {        // ...    }}</code>
Reference
https://docs.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-2.2https://docs.microsoft.com/zh-cn/aspnet/core/signalr/introduction?view=aspnetcore-2.2 
 
 
 
 
 来源:https://www.cnblogs.com/weihanli/archive/2019/09/15/use-signalr-in-aspnetcore.html
 |