首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clients.User().SendAsync()不与hubconnection.on通信

Clients.User().SendAsync()不与hubconnection.on通信
EN

Stack Overflow用户
提问于 2021-09-07 09:26:35
回答 2查看 294关注 0票数 2

这里我有blazor服务器应用程序,其中我有名为SessionChatHub的signalR集线器,它包含方法AssignmentCreatedNotification(HashSet<string> userIdList, string name),该方法接收userIdList (userIdList是从数据库AspNetUsers表访问的身份id )和名称作为参数,并将名称发送到userIdList参数的所有userid。

现在的问题是,即使在获取了userIdListname参数的值之后,也不会触发hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);,也就是说,不会调用NotifyStudentAboutAssignmentCreated(string name)

但当存在名称时,将调用NotifyStudentAboutAssignmentCreated(字符串名)方法,但它不会在await Clients.User(userId).SendAsync("ReceiveAssignmentCreatedNotification", name);上调用

userIdList:(userIdList是从数据库AspNetUsers表中访问的标识id )

下面是集线器

代码语言:javascript
复制
public class SessionChatHub:Hub
{            
    public async Task AssignmentCreatedNotification(HashSet<string> userIdList, string name)
    {
        foreach (var userId in userIdList)
        {
            await Clients.User(userId).SendAsync("ReceiveAssignmentCreatedNotification", name);
        }
    }
    public async override Task OnConnectedAsync()
    {
        Console.WriteLine($"{Context.ConnectionId} connected");
      
    }
    public override async Task OnDisconnectedAsync(Exception e)
    {
        Console.WriteLine($"Disconnected {e?.Message} {Context.ConnectionId}");
        await base.OnDisconnectedAsync(e);
    }
}

下面是我们接收signalr集线器的oninitialized

代码语言:javascript
复制
protected async override void OnInitialized()
    {
        var container = new CookieContainer();
        var cookie = new Cookie()
        {
            Name = ".AspNetCore.Identity.Application",
            Domain = "localhost",
            Value = CookiesProvider.Cookie
        };

        container.Add(cookie);


        hubConnection = new HubConnectionBuilder()
      .WithUrl(NavigationManager.ToAbsoluteUri("/sessionchathub"), options =>
      {
          options.Cookies = container;
      }).Build();
        hubConnection.ServerTimeout = TimeSpan.FromMinutes(60);


        hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);

        await hubConnection.StartAsync();
    }

要调用的方法

代码语言:javascript
复制
    public void NotifyStudentAboutAssignmentCreated(string name) // This method is not called from oninitialized  hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);
    {
        NotificationHandler.AssignmentNotificationCount = 1;
        StateHasChanged();
    } 

我也有OnAfterRenderAsync

代码语言:javascript
复制
 protected async override Task OnAfterRenderAsync(bool firstRender)
 {
    

    //if (firstRender)
    //{
    await jSRuntime.InvokeVoidAsync("AddSelect2ForClassIdInCreateEditTeacherAssignment");
    await jSRuntime.InvokeVoidAsync("AddSelect2ForSubjectIdInCreateEditTeacherAssignment");
    await jSRuntime.InvokeVoidAsync("AddSelect2ForChapterIdInCreateEditTeacherAssignment");
    //}
    await base.OnAfterRenderAsync(firstRender);
 }
EN

回答 2

Stack Overflow用户

发布于 2021-09-16 08:02:28

在没有调试的情况下,很难准确地诊断出哪里出了问题。我建议首先检查连接状态。在HubConnection类上有这个属性。

代码语言:javascript
复制
    /// <summary>
    /// Indicates the state of the <see cref="HubConnection"/> to the server.
    /// </summary>
    public HubConnectionState State => _state.OverallState;

有时创建一个端点来返回它是很有用的,这样你就可以在你的应用程序运行的时候检查它。

我看到你在OnConnectedAsync上写了一个覆盖的控制台。你检查过控制台了吗。你见过这个吗?

集线器无法连接到像不正确的url这样简单的东西。

票数 0
EN

Stack Overflow用户

发布于 2021-09-16 09:07:21

看起来SignalR身份验证有问题。可能是您从客户端发送的cookie名称或Cookie值不正确,也可能是服务器未正确配置为处理传入的身份验证cookie。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69085631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档