【问题标题】:Angular6 Typescript Signalr: Not getting any event though the Client is connected to the SignalR HubAngular6 Typescript Signalr:尽管客户端连接到 SignalR Hub,但没有收到任何事件
【发布时间】: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


【解决方案1】:

它自动工作,可能是因为缓存或其他原因,因为当我在同一页面中进行更改时,它们会反映在 UI 中。

但是直到我将事件修改为:

this.hubProxy.on('displayStatus',  (channel: string) => {
    alert('abc');
    console.log('worked', channel);
});

只要发出警报就可以刷新缓存或其他东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多