Reputation: 1
Xamarin PCL (Android
) doesn't connect to SignalR Hub
, but UWP
and WinPhone
can connect to hub. Does SignalR
support (Android
)?
I use Vs2015 and SignalR.Client(2.1.0) nuget, Microsoft.Net.Http(2.2.29) nuget, newtonsoft.Json(9.0.1) nuget, Microsoft.Bcl&Build on PCL project
I saw that, Android always disconnected but other projects can connect to signalR hub. I shared the code below. WinPhone and UWP connect the hub but android not. When emulators are loading with the project, there is no error message.
Thank you very much
//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);
}
}
Upvotes: 0
Views: 876
Reputation: 21
This could happen because the Xamarin Android client is running in an emulator, so URLs with localhost won't work.
You need to configure remote access to IIS Express and add an exception to Windows Firewall. This Xamarin walkthrough on working with WCF includes a useful section describing how to do just that.
There may be other steps you need to get signalR working, hopefully this helps.
Upvotes: 2