gpt4 book ai didi

javascript - 尝试连接到 SignalR 时出现错误 204

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:32 24 4
gpt4 key购买 nike

我无法连接到在 Windows 7 下运行的 ASP.NET Core 2.0.3 应用程序中的 SignalR Hub。我使用 NuGet 的 SignalR 1.0.0-alpha1-final 作为服务器,使用 signalr-client-1.0.0-alpha2-final.min.js 作为 JavaScript 客户端。

这是我的中心:

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}");
}
}
}

在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?}");
});
}

在测试页面中:

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);

但是当执行此代码时,我从服务器收到错误 204。

更新

根据@Gabriel Luci 的回答,这里是工作代码:

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 ");

最佳答案

GitHub 中为此提出了一个问题:https://github.com/aspnet/SignalR/issues/1028

显然,WebSockets 在 IIS 和 IIS Express 中不起作用。您需要使用长轮询。该问题中有一段示例代码:

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

关于javascript - 尝试连接到 SignalR 时出现错误 204,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47573121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com