我在winform中有一个SignalR项目。 当我启动服务器时,应用程序将在我指定的地址处启动服务器。例如:“ http:// localhost:4951” 在客户端中,我将计算机的IP地址指定为“ http://192.169.1.101:8080/signalr”。但是,客户端无法连接到运行服务器应用程序的远程计算机,并且它捕获到异常400(无效请求)。如果我在运行服务器的本地运行客户端应用程序,那么一切都很好。 Server start Client code
服务器
try
{
_signalR = WebApp.Start<Startup>("http://localhost:4951/");
writeToLog("Server started at:http://localhost:4951/");
}
catch (Exception exception)
{
MessageBox.Show(exception.Message + " " + exception.InnerException);
}
启动
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR("/signalr",new HubConfiguration
{
EnableJSONP = true,
EnableJavaScriptProxies = false,
EnableDetailedErrors = true
});
}
客户
private async Task ConnectAsync()
{
_signalRConnection = new HubConnection("http://192.168.1.105:4951/signalr");
_signalRConnection.StateChanged += _signalRConnection_StateChanged;
_hubProxy = _signalRConnection.CreateHubProxy("CommunicationHub");
_hubProxy.On<string, string>("AddMessage", (name, message) => writeToLog($"{name}:{message}"));
btnConnect.Enabled = false;
try
{
await _signalRConnection.Start();
await _hubProxy.Invoke("RegisterClient", "T001");
btnDisconnect.Enabled = true;
}
catch (Exception ex)
{
writeToLog($"Error:{ex.Message}");
btnConnect.Enabled = true;
}
}
private void _signalRConnection_StateChanged(StateChange obj)
{
if (obj.NewState == ConnectionState.Connected)
writeToLog("Connected");
else if (obj.NewState == ConnectionState.Disconnected)
writeToLog("Disconnected");
}
客户端尝试连接到远程服务器时发生异常
Disconnected
Error:StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1,
Content: System.Net.Http.StreamContent, Headers:
{
Connection: close
Date: Tue, 28 Jan 2020 05:52:24 GMT
Server: Microsoft-HTTPAPI/2.0
Content-Length: 334
Content-Type: text/html; charset=us-ascii
}