简体   繁体   中英

Error 204 when trying to connect to SignalR

I'm not able to connect to my SignalR Hub in a ASP.NET Core 2.0.3 application running under Windows 7. I'm using SignalR 1.0.0-alpha1-final from NuGet as server and the signalr-client-1.0.0-alpha2-final.min.js as JavaScript client.

Here is my hub:

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace MyProject
{
    public class MyHub: Hub
    {
        public override async Task OnConnectedAsync()
        {
            await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} joined");
        }

        public Task Send(string message)
        {
            return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
        }    
    }
}

Configure in startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseStaticFiles();
    app.UseHangfireDashboard();
    app.UseHangfireServer();

    app.UseSignalR(routes =>
    {
        routes.MapHub<MyHub>("hubs");
    });

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action=Index}/{id?}");
    });
}        

and in a test page:

let transportType = signalR.TransportType[getParameterByName('transport')] || signalR.TransportType.WebSockets;
let http = new signalR.HttpConnection(`http://${document.location.host}/hubs`, { transport: transportType });
var connection = new signalR.HubConnection(http);

but when this code executes I get an error 204 from the server.

UPDATE

Based upon the answer of @Gabriel Luci, here the working code:

let transportType = signalR.TransportType.LongPolling;
let http = new signalR.HttpConnection(`http://${document.location.host}/hubs`, { transport: transportType });
let connection = new signalR.HubConnection(http);

connection.start();

connection.on('Send', (message) => {
    console.log(message);
});

...
connection.invoke('Echo', "Hello ");

There was an issue raised in GitHub for that: https://github.com/aspnet/SignalR/issues/1028

Apparently WebSockets doesn't work in IIS and IIS Express. You need to use long-polling. There's a snippet of sample code in that issue:

let connection = new HubConnection("someurl", { transport: signalR.TransportType.LongPolling });
connection.start().then(() => {});

In my case i have to remove allow any origin and replace it with WithOrigins and AllowCredentials

 options.AddPolicy("policy",
            builder =>
            {
                builder.WithOrigins("http://localhost:8081");
                builder.AllowAnyMethod();
                builder.AllowAnyHeader();
                builder.AllowCredentials();

            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM