【问题标题】:Websocket error during connection between .net core 3.1 and Angular with Signalr [closed].net core 3.1和Angular与Signalr之间的连接期间出现Websocket错误[关闭]
【发布时间】:2020-07-10 07:42:46
【问题描述】:

我创建了一个 .net core web api 项目并尝试使用 SignalR 并连接到 Angular 应用程序。

我在与 Angular 建立连接时遇到错误。

WebSocketTransport.js:85 WebSocket connection to 'wss://localhost:44363/api/message' failed: Error during 
WebSocket handshake: Unexpected response code: 200

我的启动.cs

namespace SignalR_server
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder => builder
                .WithOrigins("http://localhost:4200")
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
            });

            services.AddSignalR();
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors("CorsPolicy");


            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<MessageHub>("/message");
            });
        }                                                                                           
    }
}

和 MessageController.cs

public class MessageController : ControllerBase
    {
        private IHubContext<MessageHub> _myHub; 

        public MessageController(IHubContext<MessageHub> hub)
        {
            _myHub = hub;
        }

        [HttpGet]
        public Message Get()
        {
            Message msg = new Message();
            msg.Id = 1;
            msg.Name = "name";
            msg.Msg = "This is a test message";
            _myHub.Clients.All.SendAsync("transfermessage", msg);
            return msg;
        }

在我的客户端连接中,我将 skipNegotiation 设为 true

  public startConnection = () => {
    this.hubConnection = new signalR.HubConnectionBuilder()
                            .withUrl('https://localhost:44363/api/message',{
                              skipNegotiation: true,
                              transport: signalR.HttpTransportType.WebSockets
                            })
                            .build();

我错过了什么?

【问题讨论】:

  • 为什么是/api/message?你的路径似乎是/message

标签: c# angular asp.net-core websocket signalr


【解决方案1】:

当您使用代码endpoints.MapHub&lt;MessageHub&gt;("/message") 时,您将“/message”设置为您的完整路径。因此,您的信号中心端点将是“https://localhost:44363/message”,而不是“https://localhost:44363/api/message”。

以后当您看到“预期 200”时,请务必检查浏览器开发者工具的网络标签。此选项卡将向您显示您遇到的真正错误。在这种情况下,我假设您会得到 404。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2022-01-22
    • 1970-01-01
    • 2018-04-18
    • 2016-11-05
    • 2016-03-13
    • 2019-05-25
    相关资源
    最近更新 更多