我在服务器端的 asp.net core 中使用 SignalR,在客户端使用 blazorise。在我使用 SignlaR 之前基本上只是为了聊天,现在我想在其他地方插入记录时使用它来更新表格。 我认为服务器端一切正常,因为当我在服务器上跟踪它时,它会发布正确的值但不会更新客户端的表。我不知道出了什么问题。 这是我在服务器端的代码,位于集线器中:
public async Task SendCartableUpdate(ResultData<PersonnelStationsInfo> resultData)
{
await Clients.All.SendAsync("RefreshCartable",resultData);
}
这就是我在客户端使用它的方式:
protected override async Task OnInitializedAsync()
{
//await base.OnInitializedAsync();
user = CurrentUserService.CurrentUser;
await CartableTableChangePage(1);
hubConnection = new HubConnectionBuilder()
.WithUrl(navigationManager.ToAbsoluteUri("http://localhost:15424/ProductionServiceHub"))
.Build();
hubConnection.On<ResultData<PersonnelStationsInfo>>("RefreshCartable", (_resultData) =>
{
StateHasChanged();
});
await hubConnection.StartAsync();
}
谢谢你的帮助
最后我解决了它,我很兴奋:) 这是我在服务器端更改代码的方式:
public async Task SendCartableUpdate()
{
await Clients.All.SendAsync("RefreshCartable");
}
这就是我在客户端更改代码的方式:
protected override async Task OnInitializedAsync()
{
//await base.OnInitializedAsync();
hubConnection = new HubConnectionBuilder()
.WithUrl(navigationManager.ToAbsoluteUri("http://192.168.2.72:1050/ProductionServiceHub"))
.Build();
hubConnection.On("RefreshCartable", () =>
{
CallLoadData();
StateHasChanged();
});
await hubConnection.StartAsync();
user = CurrentUserService.CurrentUser;
await CartableTableChangePage(1);
}
private void CallLoadData()
{
Task.Run(async () =>
{
await CartableTableChangePage(1);
StateHasChanged();
});
}