SignalR 向 Angular 客户端发送 POST 通知

SignalR sending POST notification to Angular Client

我正在尝试关注这个 tutorial!但是通过 postMan,当我调用 put 方法时,我得到了

Could not get any response

当我打电话给 https://localhost:5001/api/notify.

我的路线:

app.UseSignalR(routes =>
            {
                routes.MapHub<MessageHub>("/message");
                routes.MapHub<NotifyHub>("/notify");
            });

通知控制器:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using RealTimeMessage_Server.Models;
using RealTimeMessages.HubConfig;

namespace RealTimeMessage_Server.Controllers
{
    [Route("api/[controller]")]
    public class NotificationController : Controller
    {
        private readonly IHubContext<NotifyHub> _hub;

        public NotificationController(IHubContext<NotifyHub> hubContext)
        {
            _hub = hubContext;
        }

        public IActionResult Post([FromBody]NotificationModel msg)
        {
            _hub.Clients.All.SendAsync(msg.Type, msg.Payload);

            return Ok();
        }
    }
}

通过客户端应用程序,我可以建立连接,但仅此而已,如果我尝试启动侦听器,它还会显示

Not found for https://localhost:5001/api/notify

我的客户端APP:

private hubNotificationConnection: signalR.HubConnection;

public startConnectionNotification = () => {
    this.hubNotificationConnection = new signalR.HubConnectionBuilder()
      .withUrl('https://localhost:5001/notify')
      .build();

    this.hubNotificationConnection
      .start()
      .then(() => console.log('Notification connection started!'))
      .catch(err => console.log('Error while starting connection: ' + err))
  }

public addTranferNotificationListener = () => {
    this.hubNotificationConnection.on("BroadcastMessage", (type: string, payload: string) => {
      console.log("Type: " + type);
      console.log("Payload: " + payload);
    });
  }

private startHttpNotificationRequest = () => {
    this.http.get('https://localhost:5001/api/notify')
      .subscribe(res => {
        console.log(res);
      })
  }

ngOnInit() {
    this.startConnectionNotification();
    this.addTranferNotificationListener();
    this.startHttpNotificationRequest();
  }

经过深入研究,我发现问题是:

app.UseHttpsRedirection();

查看控制台后,我看到了以下状态:

The application completed without reading the entire request body.

所以我研究了这个问题并发现了这个

所以将上面的代码更改为这样有助于我调用 post 请求并在客户端中显示通知。

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseHttpsRedirection();
}