我只是玩了一点signalR。我的应用程序只有一个存储在 ASP.NET 应用程序中的简单集线器,我写了一个 WPF 客户端 ,它通过 hubconnection 和创建的代理与 ASP.NET 应用程序交互。在我的本地 PC 上一切正常。我部署了 ASP.NET 应用程序 在 IIS .
现在我进入正题...
当我在浏览器中输入以下内容时 我自己的 PC(pcthi-and)
http://pcthi-and:8080/signalr/hubs
我会得到我想要的
当我在 的浏览器中输入相同的 url 时另一个 电脑我会得到相同的响应,一切看起来都很好。
但是我的应用程序只能在我的电脑上运行,而不能在另一台电脑上运行。当我在另一台电脑上启动 hubconnection 时,我没有得到 connectionId。
我试图将 url 更改为我的 IP 地址,但没有效果。
浏览器对集线器的调用有效,但应用程序不起作用。
来电看起来像这样:
private bool tryToConnectToCoffeService()
{
try
{
this.hubConnection = new HubConnection(ConfigurationManager.ConnectionStrings["coffeeConnection"].ConnectionString);
this.hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
this.coffeeService = this.hubConnection.CreateHubProxy("coffee");
this.hubConnection.Start();
if (string.IsNullOrEmpty(hubConnection.ConnectionId))
{
return false;
}
return true;
}
catch(Exception ex)
{
return false;
}
}
Global.asax:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
}
这样的枢纽
[HubName("coffee")]
public class CoffeeHub : Hub
{
我的集线器连接字符串是这样的:
"http://pcthi-and:8080/"
或者:
"http://My-Current-IP-Address:8080/"
我使用 SignalR 1.0 rc2。
有没有人有想法?谢谢你的帮助。
干杯
坦率
最佳答案
我认为你需要改变
hubConnection.Start();
到
hubConnection.Start().Wait();
如果您运行的是 .NET 4.5,您可以制作
tryToConnectToCoffeService
方法 async
然后 await
当您启动集线器连接时。await hubConnection.Start();
今天它可能在本地主机上工作,因为客户端可以在
if (string.IsNullOrEmpty(hubConnection.ConnectionId))
之前完成连接执行。当您不等待
HubConnection.Start()
时,从暴露竞争条件的另一台机器连接可能需要更长的时间。去完成。
关于asp.net-mvc-4 - 当 IIS 在不同系统上运行时,SignalR WPF 客户端无法访问部署在 IIS 上的集线器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836065/