尝试使用asp.net core的信号器库从中心发布客户端方法中的事件时出错
我使用asp.net核心信号器库从服务器获取实时数据。 我正在客户端使用aurelia和typescript 在客户端的receive方法中,我发布了一个事件,这样无论何时服务器端的数据发生变化,订阅的事件都会获取数据,而无需重新刷新页面 以下是我的客户端代码:尝试使用asp.net core的信号器库从中心发布客户端方法中的事件时出错,asp.net,asp.net-mvc,asp.net-core,signalr,aurelia,Asp.net,Asp.net Mvc,Asp.net Core,Signalr,Aurelia,我使用asp.net核心信号器库从服务器获取实时数据。 我正在客户端使用aurelia和typescript 在客户端的receive方法中,我发布了一个事件,这样无论何时服务器端的数据发生变化,订阅的事件都会获取数据,而无需重新刷新页面 以下是我的客户端代码: this.connection = new signalR.HubConnectionBuilder() .withUrl("http://localhost:5003/chat") .configureLogging(
this.connection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:5003/chat")
.configureLogging(signalR.LogLevel.Debug)
.build();
this.connection.start().then(()=>{
this.connection.on("ReceiveMessage", (event, message) => {
console.log("got the message=>",event);
this.EventAggregator.publish(event, JSON.parse(message));
});
this.connection.invoke("SendMessage").catch(err => console.error(err.toString()));
});
在服务器端,我有:
public class ChatHub : Hub
{
public async Task SendMessage()
{
System.Console.WriteLine("in send message");
string data = "[{\"Id\":6,\"Count\":0},"+
"{\"Id\":5,\"Count\":0}]";
await Clients.All.SendAsync("ReceiveMessage","event1",data);
}
}
运行时,控制台上出现以下错误:
Uncaught TypeError: Cannot read property 'publish' of undefined
at HubConnection.eval (signalRservice.ts:16)
at eval (HubConnection.js:378)
at Array.forEach (<anonymous>)
at HubConnection.invokeClientMethod (HubConnection.js:377)
at HubConnection.processIncomingData (HubConnection.js:314)
at WebSocketTransport.HubConnection.connection.onreceive (HubConnection.js:141)
at WebSocket.webSocket.onmessage (WebSocketTransport.js:171)
(anonymous) @ signalRservice.ts:16
(anonymous) @ HubConnection.js:378
HubConnection.invokeClientMethod @ HubConnection.js:377
HubConnection.processIncomingData @ HubConnection.js:314
HubConnection.connection.onreceive @ HubConnection.js:141
webSocket.onmessage @ WebSocketTransport.js:171
我甚至在事件发布之前就失去了集线器连接
有人能告诉我我做错了什么吗?我是否在错误的位置发布了事件?似乎没有初始化EventAggregator。可能是使用前没有自动注入或手动初始化
@autoinject
export class ClientHub {
constructor(private eventAggregator: EventAggregator) {
}
}
然后,由Aurelia的DI初始化EventAggregator