SignalR 无传输

SignalR no transport

正在尝试启动 signal-r hub 报告"Error: No available transports found."

但是,诊断捕获显示协商 returns 三个 可用传输。

{
    "connectionId": "IsHpe_1ETRLz-flnJ3uKPg",
    "availableTransports": [
        {
            "transport": "WebSockets",
            "transferFormats": [
                "Text",
                "Binary"
            ]
        },
        {
            "transport": "ServerSentEvents",
            "transferFormats": [
                "Text"
            ]
        },
        {
            "transport": "LongPolling",
            "transferFormats": [
                "Text",
                "Binary"
            ]
        }
    ]
}

这是用 aspnetcore 2.1 preview2 完成的,启动看起来像这样

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options => options.AddPolicy("AllowAny", x =>
          x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()));
        services.AddSignalR();
        services.AddOptions();
        services.Configure<AppSettings>(Configuration);
        var connectionStrings = Configuration.GetSection("ConnectionStrings");
        var defaultConnectionString = connectionStrings[Configuration["DefaultConnectionString"]];
        services.AddDbContext<au1.Model.ProwlerDbContext>(options => 
          options.UseSqlServer(defaultConnectionString));
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseCors("AllowAny");
        app.UseSignalR(routes =>
        {
            routes.MapHub<MessageHub>("/message");
        });
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
            {
                HotModuleReplacement = true
            });
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

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

            routes.MapSpaFallbackRoute(
                name: "spa-fallback",
                defaults: new { controller = "Home", action = "Index" });
        });
    }

显然路由有效,否则协商不会出现在网络流量中。使用浏览器调用 http://local:5000/message 会按预期生成一条消息 Connection ID required

我是不是漏了一步?

在客户端,这是我尝试连接的方式。

    this.signalr = new SignalR.HubConnection("http://localhost:5000/message");
    this.signalr.on("stateChange", (data: string) => {
        console.log(`SignalR: ${data}`);
        that.selection = data;
    });
    this.signalr.start().then(result=>{
        console.log("hub started");
    }).catch(err=>{
        console.log("hub failed to start");
    });

package.json 说 "_id": "@aspnet/signalr@1.0.0-preview1-update1",

.csproj 表示 <PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.0-preview2-final" />

这些不一样。他们是否兼容我不能说。

我不知道这些是如何不同步的,因为我省略了版本以获取每个版本的最新版本。 npm 更新将客户端带到预览版 2,我现在将重新测试。

服务器和客户端的版本必须一致。您需要通过转到 .csproj 文件(对于服务器版本 and/or C# 客户端版本)和 package.json 对于版本来确保两个客户端版本相同JavaScript/TypeScript 客户端,如果你使用 npm 安装的话。

请注意,预览版之间存在重大更改,因此您不能将 -previewX 客户端与 previewY 服务器一起使用。