带有MVC 5的Angular 6 Signalr

时间:2018-09-18 12:21:30

标签: asp.net-mvc-5 signalr angular6

我在我当前使用mvc 5的项目中使用了angular 6,其他所有东西都运行良好,这是我实现SignalR时面临的唯一问题,这是我的signalr.service.ts代码:

export class SignalRService {
dataReceived = new EventEmitter<myData>();
connectionEstablished = new EventEmitter<Boolean>();

private connectionIsEstablished = false;
private _hubConnection: HubConnection;

constructor() {
    this.createConnection();
    this.registerOnServerEvents();
    this.startConnection();
}


private createConnection() {
    this._hubConnection = new HubConnectionBuilder()
        .withUrl(window.location.href+'testHub')
        .build();

}

private startConnection(): void {
    this._hubConnection
        .start()
        .then(() => {
            this.connectionIsEstablished = true;
            console.log('Hub connection started');
            this.connectionEstablished.emit(true);
        })
        .catch(err => {
            console.log('Error while establishing connection, retrying...');
            //setTimeout(this.startConnection(), 5000);
        });
}

private registerOnServerEvents(): void {
    this._hubConnection.on('sendProgressTrackerAlert', (data: any) => {
        this.dataReceived.emit(data);
    });
}

}

但这给了我这个错误:

     Utils.js:148 Error: Failed to complete negotiation with the server: Error: Not Found
 push../node_modules/@aspnet/signalr/dist/esm/Utils.js.ConsoleLogger.log
 @ Utils.js:148 14:55:10.842 Utils.js:148 Error: Failed to start the
 connection: Error: Not Found

顺便说一句; MVC5是否可以使用Angle 6 Signalr?因为根据我的知识和搜索,我发现角度6信号器只能与Core一起使用? Shillong

1 个答案:

答案 0 :(得分:0)

如果您希望使用带有MVc5解决方案的signalR用户,则需要将jQuery.signalR模块与角度客户端一起使用,而不是@ aspnet.SignalR。