c# - SignalR HubConnectionState = 正在连接
问题描述
我正在尝试使用 Xamarin 客户端创建 SignalR 服务器。每次我尝试连接到服务器时,状态只是“正在连接”而没有改变。试图用我在互联网上找到的代码绕过证书,但它并没有解决问题。当我尝试访问集线器站点(ip:port/notifier)时,我收到消息“需要连接 ID”
客户端:
public class SignalRService : ISignalRService
{
private readonly HubConnection hubConnection;
public SignalRService()
{
hubConnection = new HubConnectionBuilder()
.WithUrl("https://ip:5001/notifier", options => {
options.HttpMessageHandlerFactory = factory => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
};
})
.WithAutomaticReconnect()
.Build();
}
public void /*async Task*/ Connect()
{
hubConnection.StartAsync();
var a = hubConnection.State;
while (hubConnection.State == HubConnectionState.Connecting) //for debug purposes
{
continue;
}
a = hubConnection.State;
}
服务器端
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapHub<NotifyHub>("/notifier");
});
}
Thanks for any help!
解决方案
所以最后我找到了解决方案!
问题是模拟器无法通过 http://localhost:port 连接到 localhost 但使用 ip "10.0.2.2:port"
并且由于某种原因向客户端实施 signalR,因为服务也无法正常工作......
推荐阅读
- openssl - suse openssl 未定义符号:private_CAST_set_key
- java - REST客户端和浏览器中Web应用程序中基本身份验证的不同处理
- javascript - Flow 中的可组合和正合约
- netflix-zuul - 有没有办法改变 netflix zuul 路由过滤器中的 http 请求方法?
- lua - 如何去混淆这个?
- python - 使用 Python 运行外部程序
- swift - Firebase 在调用下一个方法之前没有完成
- java - 如何使用 JAVA Apache POI 为 powerpoint 每张幻灯片的背景设置不同的图像?
- android - 从数据库中删除一行后如何刷新页面以显示更新的数据?
- python-3.x - 如何在“计算繁重”功能运行时停止 tkinter 窗口不可交互