这里我有blazor服务器应用程序,其中我有名为SessionChatHub的signalR集线器,它包含方法AssignmentCreatedNotification(HashSet<string> userIdList, string name),该方法接收userIdList (userIdList是从数据库AspNetUsers表访问的身份id )和名称作为参数,并将名称发送到userIdList参数的所有userid。
现在的问题是,即使在获取了userIdList和name参数的值之后,也不会触发hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);,也就是说,不会调用NotifyStudentAboutAssignmentCreated(string name)。
但当存在名称时,将调用NotifyStudentAboutAssignmentCreated(字符串名)方法,但它不会在await Clients.User(userId).SendAsync("ReceiveAssignmentCreatedNotification", name);上调用
userIdList:(userIdList是从数据库AspNetUsers表中访问的标识id )
下面是集线器
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
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();
}要调用的方法
public void NotifyStudentAboutAssignmentCreated(string name) // This method is not called from oninitialized hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);
{
NotificationHandler.AssignmentNotificationCount = 1;
StateHasChanged();
}
我也有OnAfterRenderAsync
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);
}发布于 2021-09-16 08:02:28
在没有调试的情况下,很难准确地诊断出哪里出了问题。我建议首先检查连接状态。在HubConnection类上有这个属性。
/// <summary>
/// Indicates the state of the <see cref="HubConnection"/> to the server.
/// </summary>
public HubConnectionState State => _state.OverallState;有时创建一个端点来返回它是很有用的,这样你就可以在你的应用程序运行的时候检查它。
我看到你在OnConnectedAsync上写了一个覆盖的控制台。你检查过控制台了吗。你见过这个吗?
集线器无法连接到像不正确的url这样简单的东西。
发布于 2021-09-16 09:07:21
看起来SignalR身份验证有问题。可能是您从客户端发送的cookie名称或Cookie值不正确,也可能是服务器未正确配置为处理传入的身份验证cookie。
https://stackoverflow.com/questions/69085631
复制相似问题