【发布时间】:2018-06-15 08:59:40
【问题描述】:
我已经使用 Web API 和 Angular UI 创建了一个 SignalR 集线器。
预期结果是将 Angular UI 与 SignalR 集线器连接起来,并且在服务器上发生任何事件时,都应通知 Angular 客户端相应的事件名称。
角度集线器的代码是:
public openChannel(url: string): void {
this.hubConnection = $.hubConnection(url, {"useDefaultPath": true});
this.hubProxy = this.hubConnection.createHubProxy('employeeHub');
this.hubConnection.logging = true;
**this.hubProxy.on('displayStatus', (channel: string) => {
console.log('worked', channel);
});**
this.hubConnection.start(this.options).done((data: any) => {
console.log('Connected to Processing Hub');
this.sendMessage();
}).catch((error: any) => {
console.log('Hub error -> ' + error);
});;
}
它的调用方法是:
ngOnInit(): void {
this.getLGEData();
this._signalrService.openChannel('http://localhost/RealTimeNotify/Hubs');
}
它显示控制台消息“已连接到处理中心”,但当服务器端发生某些事情时,突出显示的代码不会显示任何控制台消息。
当我们使用以下代码获取更新时,服务器端代码与 jQuery 一起工作正常:
$(function () {
// Proxy created on the fly
var emp = $.connection.employeeHub;
// Declare a function on the job hub so the server can invoke it
emp.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
如果在这种情况下需要更多信息,请告诉我。
提前致谢。
【问题讨论】:
-
角度方法
on('displayStatus', (channel: string) => {需要一个字符串参数,而jQuery 方法根本不需要任何参数。您是否尝试使角度也无参数?
标签: angular typescript signalr signalr-hub