如何使用C#中的signalR和API在Angular 7中集成推送通知?

时间:2019-04-01 08:12:42

标签: c# typescript api signalr angular7

我有一个Angular 7应用程序,我想使用SignlaR(2.4.4版本)和C#中的API来实现推送通知。任何人都可以向它提出任何文档或任何示例代码来帮助我解决问题。

我已经尝试了所有可以尝试的方法,但是我无法在Angular SignalR客户端和C#API中创建的SignalR Hub之间建立连接。

 1)This is my Startup.cs 
        HubConfiguration cfg = new HubConfiguration();
        app.MapSignalR<PersistentConnection>("/ClientHub");
        app.MapSignalR(cfg);
 2)This is my Hub
        public class ClientHub : Hub
       {
         public Task SendNotificationCount(int chatUserCnt)
         {
           var context = 
           GlobalHost.ConnectionManager.GetHubContext<ClientHub>();
           return context.Clients.All.SendAsync("Send", chatUserCnt);
         }

         public Task SendHelloToAll(string message)
         {
            var context = 
            GlobalHost.ConnectionManager.GetHubContext<ClientHub>();
            return context.Clients.All.SendMsgAsync("SendToAll", message);
          }

        }
   3)This is Angular Code from there i am trying to Connect to Hub.
       hubUrl = http://localhost:60067/ClientHub
       connectToHub(hubUrl:string){

       this.connection = new 
       HubConnectionBuilder().withUrl(hubUrl).build();

       this.connection.start().then(() => {
       console.log("connected to hub");
       this.connection.on('SendToAll', (message) => { 
       alert(message);
       });
       }).catch((error) => {
        console.log(error);
       });


      }


 I am expecting, whenever there is change in Database for notification i 
 want to get that alerts through Hub realtime.
 Also i want to send database changes using signalR hub to angular.
 But i am not able to connect with hub from angular.