无法发送通知给客户端

时间:2019-10-14 08:09:25

标签: c# .net angular signalr

在我的.net核心应用程序中,我想在.net核心服务器上发生事件时向用户发送通知。我的客户端位于Angular中,并且我正在使用signalR在客户端和服务器之间进行通信。我能够建立基本的SignalR连接,但是我对如何在signalR中订阅事件并在事件完成时通知我的用户感到困惑。事件完成后,我无法向客户端发送通知。我收到一个错误错误:由于服务器上的错误,无法调用“数据”。有人可以指导我解决问题吗,我该如何解决

Appcomponent.ts

 ngOnInit() {
   this._hubConnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Information)
.withUrl("http://localhost:5000/message", this.transportType)
.build();
}
form(){
this._hubConnection.on("Data", (d1) => {
  const received = `message: ${d1}`;
  console.log(received);
});

this._hubConnection.start()
.then(() =>
this._hubConnection.invoke('Data',"d1").catch(function (err) {

  console.error(err.toString());
})
)
.then(() =>
console.log('connected'))
.then(() => this._hubConnection.stop())
.then(() => console.log('Disconnected'))
.catch(e => console.error(e.message));
}

控制器

namespace ang.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DrawingController : Controller
    {

        private IHubContext<DamHub> _hubContext;
        public DamController(IHubContext<DamHub> hubContext)
        {
            _hubContext = hubContext;
}


        }
// event
private void Data1(DataAndHeader d1)
        {
            CommHeader header = (CommHeader)dh.Header;

            switch (header.Type)
            {
                case CommHelpers.CommType.ServerToClientReceived:
                    break;
                case CommHelpers.CommType.ServerToClientFinished:


                    string ClientID = header.ClientID;
                    ConcurrentQueue<DataAndHeader> queueResultsForClient = null;
                    if (!dicResults.TryGetValue(ClientID, out queueResultsForClient))
                    {
                        queueResultsForClient = new ConcurrentQueue<DataAndHeader>();
                        if (!dicResults.TryAdd(ClientID, queueResultsForClient))
                        {
 _hubContext.Clients.All.SendAsync("Data", d1); 
                        }
                    }
                    queueResultsForClient.Enqueue(d1);
                    break;

                  default:
                    break;
            }


}
````


0 个答案:

没有答案