HubServer.Program.cs:
public static void Main(string[] args)
{
getRegistrySettings();
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
builder.Services.AddCors(options =>
{
options.AddPolicy("CORSPolicy", builder =>
builder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().SetIsOriginAllowed((hosts) => true));
});
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//CORS need to be enabled for calling SignalR service
app.UseCors("CORSPolicy");
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapHub<MCHub>("/mchub");
//app.MapFallbackToPage("/_Host");
app.Run();
}
要访问集线器,我用途:
WPF App:
public MainWindow()
{
InitializeComponent();
//const string url = "http://localhost:5289/mchub";
const string url = "http://192.168.10.120/mcsrhub/mchub";
var queryString = new Dictionary<string, string>
{
{ "CreatedBy", "me" },
{ "UserId", "1234" },
{ "MachineName", Environment.MachineName }
};
var newUrl = new Uri(QueryHelpers.AddQueryString(url, queryString));
connection = new HubConnectionBuilder()
.WithUrl(newUrl) //"http://localhost:5289/mchub") //5181/chathub")
.WithAutomaticReconnect()
.Build();
connection.Reconnecting += (sender) =>
{
this.Dispatcher.Invoke(() =>
{
var newMessage = "Attempting to reconnect...";
messages.Items.Add(newMessage);
});
return Task.CompletedTask;
};
connection.Reconnected += (sender) =>
{
this.Dispatcher.Invoke(() =>
{
var newMessage = "Reconnected to the server";
messages.Items.Clear();
messages.Items.Add(newMessage);
});
return Task.CompletedTask;
};
connection.Closed += (sender) =>
{
this.Dispatcher.Invoke(() =>
{
var newMessage = "Connection Closed";
messages.Items.Add(newMessage);
openConnection.IsEnabled = true;
sendMessage.IsEnabled = false;
});
return Task.CompletedTask;
};
}
private async void openConnection_Click(object sender, RoutedEventArgs e)
{
connection.On<string, string>("ReceiveMessage", (user, message) =>
{
this.Dispatcher.Invoke(() =>
{
var newMessage = $"{user}: {message}";
messages.Items.Add(newMessage);
});
});
try
{
await connection.StartAsync();
messages.Items.Add("Connection Started");
openConnection.IsEnabled = false;
sendMessage.IsEnabled = true;
}
catch (Exception ex)
{
messages.Items.Add(ex.Message);
}
}
private async void sendMessage_Click(object sender, RoutedEventArgs e)
{
try
{
await connection.InvokeAsync("SendMessage",
"WPF Client", messageInput.Text);
}
catch (Exception ex)
{
messages.Items.Add(ex.Message);
}
}
如果我使用localhost作为url,则连接正常。当我在LAN上的另一台机器上使用相同的WPF应用程序并使用服务器IP地址或服务器MachineName时,我得到:
- 连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机没有响应。(192.168.10.120:80)*
我已经禁用了两台机器上运行的AVG,并检查了服务器上的端口80是否打开。我添加了cors引用,但这没有什么区别。
编辑:由IIS托管。
2条答案
按热度按时间dauxcl2d1#
我所拥有的更复杂知识的人将知道您可以配置IIS将侦听的端口。我知道默认值应该是空的,这意味着它将侦听所有端口,但我的设置为“::”,这意味着所有IPv6端口。“localhost”监听IPv6,但它显然会自动将IPv4转换为IPv6(这意味着代码内置在OS:-)中)。
要在IPv4上侦听,您需要“0.0.0.0”。
执行“netsh http add iplisten ipaddress 192.168.10.120“为我修复了它。This website has details。
ux6nzvsh2#
为了凯斯特瑞尔
如果您使用的是Kestrel,您可以检查我的答案,并按照步骤来解决问题。
对于IIS
如果您在IIS中托管应用程序,您应该使用您的PC ping
192.168.10.120
机器(如果不能ping,您应该检查192.168.10.120服务器的PC,如果启用了ICMP)。然后仔细检查inbound rule in your server。