我正试图在.NET控制台中使用SignalR集线器和SignalR客户端构建示例解决方案。
服务器代码:
class Server
{
static void Main(string[] args)
{
string url = "http://localhost:8080/";
using (WebApp.Start(url))
{
MyHub hub = new MyHub();
Console.WriteLine("Server running on {0}", url);
var key = Console.ReadLine();
while (key != "quit")
{
hub.Send("Server", key);
key = Console.ReadLine();
}
}
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.addMessage(name, message);
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
客户端代码:
class Client
{
static void Main(string[] args)
{
MainAsync().Wait();
Console.ReadLine();
}
static async Task MainAsync()
{
try
{
var hubConnection = new HubConnection("http://localhost:8080");
// Does not work although the server at 192.168.5.35 is listening on port 8080
// var hubConnection = new HubConnection("http://192.168.5.35:8080");
IHubProxy hubProxy = hubConnection.CreateHubProxy("MyHub");
hubProxy.On<string, string>("addMessage", (name, message) =>
{
Console.WriteLine("Incoming data: {0} {1}", name, message);
});
ServicePointManager.DefaultConnectionLimit = 10;
await hubConnection.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
用法:运行服务器,然后运行客户端。在服务器控制台中,输入消息字符串。消息通过SignalR传送到客户端控制台,并在其中显示。
此代码按原样运行。但是,只有网址是本地主机:两侧都是8080。
我需要在两台不同的机器上安装服务器和客户端,所以我试着让服务器监听localhost:8080,并且客户端连接到服务器端口8080上的本地IP地址(即{{ 3}})
在这种情况下,hubConnection.Start()会抛出以下异常:
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:
System.Net.Http.StreamContent, Headers:
{
Connection: close
Date: Thu, 16 Mar 2017 18:15:19 GMT
Server: Microsoft-HTTPAPI/2.0
Content-Length: 334
Content-Type: text/html; charset=us-ascii
}
我发现某个地方的网页框是强制性的,所以我在IIS上安装了它
我禁用了我的防火墙
问题仍然存在
有人可以帮忙吗?