我试图利用这篇文章: https://codingblast.com/asp-net-core-signalr-chat-angular/ 并使用角度5连接到signalR hub。但是当我运行项目时,我在控制台中看到此消息:
错误:无法启动连接。 SyntaxError:JSON输入的意外结束 app.component.ts:24建立连接时出错:(
这是我的组件:
import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr-client';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
private _hubConnection: HubConnection;
nick = '';
message = '';
messages: string[] = [];
public getRealTime(): void {
this._hubConnection
.invoke('getRealTime')
.catch(err => console.error(err));
}
ngOnInit() {
this._hubConnection = new HubConnection('/clockHub');
this._hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection :('));
this._hubConnection.on('setRealTime', (time: string) => {
alert(time);
});
}
}
以及hub类的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace clockHub
{
public class ClockHub : Hub
{
public void GetRealTime()
{
Clients.Caller.setRealTime(DateTime.Now.ToString("h:mm:ss tt"));
}
}
}
谢谢。