简体   繁体   English

信号 R - OnDisconnectedAsync(异常?异常) Function 不工作

[英]Signal R - OnDisconnectedAsync(Exception? exception) Function is Not working

I'm working on a chat application on the web using blazor web assemblly我正在使用 blazor web 组件在 web 上开发聊天应用程序

I'm tring to push the states(Online / Offline) of users to each others.我正在尝试将用户的状态(在线/离线)相互推送。

在此处输入图像描述

here is the signalRHub这是信号RHub

在此处输入图像描述

On connected is working fine.在连接上工作正常。

public override Task OnConnectedAsync()公共覆盖任务 OnConnectedAsync()

My problem is that OnDisconnectedAsync() not getting hitted by the debuger at all我的问题是 OnDisconnectedAsync() 根本没有被调试器击中

Even after I close the client(browser) that the server is connected with.即使我关闭了与服务器连接的客户端(浏览器)。

    public override Task OnDisconnectedAsync(Exception? exception)
    {
        var userId = _userManager.GetUserId(Context.User);


        return base.OnDisconnectedAsync(exception);
    }

You need to configure the middleware for the token on the server for the signalr hubs.您需要为 signalr 集线器的服务器上的令牌配置中间件。

services.AddAuthentication().AddIdentityServerJwt();

services.TryAddEnumerable(
    ServiceDescriptor.Singleton
        <IPostConfigureOptions<JwtBearerOptions>,ConfigureJwtBearerOptions>());
public class ConfigureJwtBearerOptions : IPostConfigureOptions<JwtBearerOptions>
{
    public void PostConfigure(string name, JwtBearerOptions options)
    {
        var originalOnMessageReceived = options.Events.OnMessageReceived;
        options.Events.OnMessageReceived = async context =>
        {
            await originalOnMessageReceived(context);

            if (string.IsNullOrEmpty(context.Token))
            {
                var accessToken = context.Request.Query["access_token"];
                var path = context.HttpContext.Request.Path;

                if (!string.IsNullOrEmpty(accessToken) &&
                    path.StartsWithSegments("/hubs"))
                {
                    context.Token = accessToken;
                }
            }
        };
    }
}

NOTE : This assumes your signalr hub(s) endpoint(s) start with "/hubs"注意:这假设您的 signalr 集线器端点以“/集线器”开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM