我正在研究ASP.NET Core 2.2 SignalR应用程序。
服务器正在通知客户端(下面的onFreshData())。我希望在客户端上调用的方法可以在服务器上回调另一个方法。
从某种意义上说,这似乎不受支持,如果正确调用服务器代码,则客户端代码(打字稿)会出错。
这是从服务器调用的方法:
onFreshData = (data: any): void => {
const received = `Données reçues: ${data}`;
this.messages.push(received);
const dashboardData = DashboardDataProxy.Parse(data);
// Build a ticks array
const almostTicks: number[] = [];
const alerts = dashboardData.alerts;
alerts.forEach(a => almostTicks.push(moment.utc(a.AlertDateTime).valueOf() * 10000));
// Call the server method for formatting
this.notificationService.formatDateTimeArrayOnServer(almostTicks, 'fr-FR', moment().utcOffset())
.then(() => { this.chartService.updateAlertHistory(alerts); });
;
}
以下是回调服务器的客户端代码:
public async formatDateTimeArrayOnServer(almostUtcTicks: number[], currentCulture: string, utcOffset: number) {
if (this.hubConnection) {
let result = await this.hubConnection.invoke('FormatDateTimeArray', almostUtcTicks, currentCulture, utcOffset);
return result;
}
}
Chrome调试器中的result值为:
“结果未定义” “ ReferenceError:结果未定义 在评估时(在push ../ src / app / services / notification.service.ts.NotificationService.formatDateTimeArrayOnServer(https://localhost:44338/main.js:5116:12),: 1:1时评估) 在NotificationService.push ../ src / app / services / notification.service.ts.NotificationService.formatDateTimeArrayOnServer(https://localhost:44338/main.js:5116:12)中 在HubConnection.NotificationComponent.onFreshData(https://localhost:44338/src-app-components-dashboard-dashboard-module.js:448:39) 在https://localhost:44338/vendor.js:139594:53 在Array.forEach() 在HubConnection.push ../ node_modules/@aspnet/signalr/dist/esm/HubConnection.js.HubConnection.invokeClientMethod(https://localhost:44338/vendor.js:139594:21)中 在HubConnection.push ../ node_modules/@aspnet/signalr/dist/esm/HubConnection.js.HubConnection.processIncomingData(https://localhost:44338/vendor.js:139489:30)中 在WebSocketTransport.HubConnection.connection.onreceive(https://localhost:44338/vendor.js:139225:68) 在WebSocket.webSocket.onmessage上[作为__zone_symbol__ON_PROPERTYmessage](https://localhost:44338/vendor.js:140902:43) 在WebSocket.wrapFn(https://localhost:44338/polyfills.js:3677:39)”