【发布时间】:2018-11-15 18:53:54
【问题描述】:
简单的情况。如何使用我计算机中的客户端连接到我的远程桌面服务器(启用端口)中的 SignalR 服务器应用程序。在本地主机中连接工作正常,只要我输入远程计算机 IP,它就会给出错误 400。
服务器端:
namespace SignalRHub
{
class Program
{
static void Main(string[] args)
{
string url = @"http://localhost:8080/";
using (WebApp.Start<Startup>(url))
{
Console.WriteLine(string.Format("Server running at {0}", url));
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
[HubName("TestHub")]
public class TestHub : Hub
{
public void DetermineLength(string message)
{
Console.WriteLine(message);
string newMessage = string.Format(@"{0} has a length of: {1}", message, message.Length);
Clients.All.ReceiveLength(newMessage);
}
}
}
客户端
namespace SignalRClient
{
class Program
{
static void Main(string[] args)
{
IHubProxy _hub;
//string url = @"http://localhost:8080/";
string url = @"http://111.11.11.111:8080";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
try
{
connection.Start().Wait();
Console.WriteLine("Connection OK. Connected to: "+url);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
throw;
}
_hub.On("ReceiveLength", x => Console.WriteLine(x));
string line = null;
while ((line = System.Console.ReadLine()) != null)
{
_hub.Invoke("DetermineLength", line).Wait();
}
Console.Read();
}
}
}
它给出的错误:
“System.AggregateException: 发生一个或多个错误。---> Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 400, ReasonPhrase: 'Bad Request'”
我知道有类似的主题,但由于我只熟悉 C# 控制台和 Windows 应用程序,因此很高兴找到一种将应用程序连接到应用程序的解决方案。我的 RDP 服务器是完全可访问的,我在那里运行着数据库和其他服务,所以问题显然出在代码中。顺便说一下,我在帖子中更改了IP,所以它不是真实的.. 附言如果我在服务器端使用 url = @"http://*8080/" ,编译器会给出 "HttpListenerException: Access is denied" ...
【问题讨论】:
标签: c# winforms console signalr