Xamarin PCL (Android)不能连接到SignalR Hub,但UWP和WinPhone可以连接到hub。SignalR是否支持(Android)?
我在PCL项目中使用Vs2015和SignalR.Client(2.1.0) nuget、Microsoft.Net.Http(2.2.29) nuget、newtonsoft.Json(9.0.1) nuget、Microsoft.Bcl&Build
我看到,安卓总是断开连接,但其他项目可以连接到signalR集线器。我分享了下面的代码。WinPhone和UWP连接集线器,但android不连接。当模拟器与项目一起加载时,不会出现错误消息。
非常感谢
//Server:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Hosting;
using Owin;
...
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
public override Task OnConnected()
{
Program.MainForm.WriteToConsole("Client connected: " + Context.ConnectionId);
return base.OnConnected();
}
public override Task OnDisconnected()
{
Program.MainForm.WriteToConsole("Client disconnected: " + Context.ConnectionId);
return base.OnDisconnected();
}
}
//Starting Methot on WinForm
private IDisposable SignalR { get; set; }
private void ButtonStart_Click(object sender, EventArgs e)
{
SignalR = WebApp.Start("http://localhost:8080");
}
//On PCL code
//HomePage.xaml
<StackLayout>
<Entry x:Name="MainEntry"/>
<Button x:Name="btnSend" Clicked="Button_OnClicked" Text="Send Entry"/>
<Label x:Name="MainLabel"/>
<ListView x:Name="MainListview"/>
</StackLayout>
//HomePage.xaml.cs
using Microsoft.AspNet.SignalR.Client;
...
{
private String UserName { get; set; }
private IHubProxy HubProxy { get; set; }
const string ServerURI = "http://localhost:8080";
private HubConnection connection { get; set; }
public HomePage()
{
InitializeComponent();
Device.BeginInvokeOnMainThread(() => ConnectAsync());
}
private async Task ConnectAsync()
{
connection = new HubConnection(ServerURI);
HubProxy = connection.CreateHubProxy("MyHub");
HubProxy.On<string, string>("AddMessage", (name, message) =>MainLabel.Text+= String.Format("{0}: {1}" + Environment.NewLine, name, message));
await connection.Start();
}
private void Button_OnClicked(object sende, EventArgs e)
{
HubProxy.Invoke("Send", UserName, MainEntry.Text);
}
}发布于 2016-12-14 23:32:02
这可能是因为Xamarin Android客户端运行在模拟器中,所以带有localhost的URL将不起作用。
您需要配置对IIS Express的远程访问,并向Windows防火墙添加例外。这个关于使用WCF的Xamarin演练包括a useful section describing how to do just that.。
可能还有其他步骤需要让signalR正常工作,希望这会有所帮助。
https://stackoverflow.com/questions/38061374
复制相似问题