使用不同的 IP 地址连接 Signalr Hub 和客户端

Connect Signalr Hub and Client using different IP address

在 VS 2019 中工作。

我是 SignalR 的新手,我编写了 Hub/Client 应用程序,但我无法使用两台不同 IP 的 PC 在我的本地家庭 LAN 上工作。

所以我回到基础并加载了示例聊天应用程序,但我遇到了同样的问题。

我正在使用来自 github here

的演示聊天示例

PC A(我的工作 PC)作为 SignalR Hub 运行连接聊天中心 (192.168.0.2)。

PC B(我的笔记本电脑)运行聊天 'Windows Forms Sample' 客户端 (192.168.0.9)

防火墙:我在两台 PC 上都允许端口 52995 的入站和出站规则。

集线器连接:

string ip = "localhost";
string port = "52995";

var url = new Uri($"http://{ip}:{port}/chat");

App.HubConn = new HubConnectionBuilder()
           .WithUrl(url, options =>
           {                   
           })
           .Build();

如果我 运行 Winforms 客户端与集线器 (localhost) 在同一台 PC 上,那么一切正常,但在笔记本电脑上,我得到的 error 是:

System.Net.HttpRequestException. Response code does not indicate success: 400 (Bad Request)

为清楚起见,Winforms 聊天客户端应用程序允许您在文本框中输入 url:

http://localhost:52995/chat   ----> client running on localhost   -> works
http://192.168.0.2:52995/chat ----> client running on 192.168.0.9 -> error

我已经尝试解决这个问题一天多了,为什么它不起作用?

回答我自己的问题...

我的用例有点不同。

我需要 运行 一台 windows 10 台 PC 上的服务器集线器,不超过十几个本地 (LAN) 客户端(所以它都是本地 LAN,没有云)。 Kestral 似乎是在这种情况下使用的服务器。

我了解到 IIS Express 仅处理 localhost 流量。

为了测试,我在 VitrualBox 虚拟机(用于服务器集线器)中 运行 复制了 Visual Studio,这意味着 VS 使用的是 IIS Express,因此不允许本地主机之外的流量,因此400.

让它工作

使用 CLI 启动网络应用程序 - 这将使用 Kestral 作为 唯一 网络服务器(通常 IIS 代理 http 流量 to/from Kestral)

dontnet run <your app name>

我还将托管模型设置为 OutOfProcess(在 csproj 中)

<PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>OuOfProcess</AspNetCoreHostingModel>
</PropertyGroup>

虽然我不确定这是否真的有什么不同,因为看起来 dotnet 运行 可以在这种托管模式下工作。