无法连接到Hub

时间:2014-02-22 03:31:02

标签: c# windows-phone-8 signalr

在我的服务器端,我只有一个由MVC构成的简单服务器端

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <directoryBrowse enabled="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

public class Chat : Hub
{
    public void Send(string platform, string message)
    {
        Clients.All.messageReceived(platform, message);
    }
}

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

我的Windows手机客户端

public class Client
{
    private readonly string _platform;
    private readonly HubConnection _connection;
    private readonly IHubProxy _proxy;

    public event EventHandler<string> OnMessageReceived;

    public Client(string platform)
    {
        _platform = platform;
        _connection = new HubConnection("http://localhost");
        _proxy = _connection.CreateHubProxy("Chat");

    }

    public async Task Connect()
    {
         await _connection.Start();

        _proxy.On("messageReceived", (string platform, string message) =>
        {
            if (OnMessageReceived != null)
                OnMessageReceived(this, string.Format("{0}: {1}", platform, message));
        });

        await Send("Connected");
    }

    public Task Send(string message)
    {
        return _proxy.Invoke("Send", _platform, message);
    }
}

在我的 MainWindow.xaml.cs

_client = new Client("Windows");

在我部署Connect之后调用Hub时,它会抛出一个异常,但响应为

{StatusCode: 404, ReasonPhrase: '', Version: 0.0, Content: System.Net.Http.StreamContent, Headers: { Content-Length: 0 }}

我在这里遗漏了什么吗?我还检查了reference如何解决问题,但我似乎对他们的指导方针没问题。

1 个答案:

答案 0 :(得分:0)

Windows Phone 8模拟器是一个虚拟机。这意味着“localhost”引用VM / phone,而不是运行IIS / IISExpress的主机。

您可以使用其主机名或IP地址连接到主机。

如果您决定坚持使用IIS Express,则需要将Windows防火墙配置为接受运行IIS Express的任何端口上的入站请求。

How to connect to a local web service from the Windows Phone 8 emulator上的指南侧重于WCF,但同样适用于SignalR。