【发布时间】:2016-10-29 21:43:35
【问题描述】:
Xamarin PCL (Android) 无法连接到 SignalR Hub,但 UWP 和 WinPhone 可以连接到集线器。 SignalR 支持 (Android) 吗?
我使用 Vs2015 和 SignalR.Client(2.1.0) nuget、Microsoft.Net.Http(2.2.29) nuget、newtonsoft.Json(9.0.1) nuget、Microsoft.Bcl&Build on PCL 项目
我看到,Android 总是断开连接,但其他项目可以连接到 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);
}
}
【问题讨论】:
-
您是否收到任何类型的错误消息、堆栈跟踪、日志猫、瓶中消息等来帮助确定它为什么无法连接?越详细越好。
-
请发布相关代码和更多关于你在做什么的细节。您的问题不包含我们可以用来帮助您的任何有意义的细节。
-
非常感谢。我编辑了我的帖子。