是可以初始化HubConnection的Microsoft.AspNetCore.TestHost.TestServer?
下面的示例在以下位置抛出HttpRequestException(未找到)异常await hubConnection.StartAsync();
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.TestHost;
using Xunit;
namespace FunctionalTests
{
public class PubSubScenarios
{
[Fact]
public async Task SignalRHubTest_Foo()
{
var webHostBuilder = WebHost.CreateDefaultBuilder().UseStartup<Startup>();
using (var testServer = new TestServer(webHostBuilder))
{
var hubConnection = await StartConnectionAsync(testServer.BaseAddress);
}
}
private static async Task<HubConnection> StartConnectionAsync(Uri baseUri)
{
var hubConnection = new HubConnectionBuilder()
.WithUrl($"http://{baseUri.Host}/fooHub")
.WithConsoleLogger()
.Build();
await hubConnection.StartAsync();
return hubConnection;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个 ASP.NET Boilerplate v3.6.2 项目(.NET Core / Angular),其中我需要从后端方法调用客户端函数,因此我使用 ASP.NET Core SignalR 实现。
我按照官方文档,所以:
在后台
在我的模块中,我将依赖项添加到AbpAspNetCoreSignalRModule:
[DependsOn(typeof(AbpAspNetCoreSignalRModule))]
public class MyModule : AbpModule
Run Code Online (Sandbox Code Playgroud)
并将此NuGet 包添加到模块的项目中。
然后我扩展了该类AbpCommonHub以利用 SignalR Hub 的内置实现,添加SendMessage()用于发送消息的方法:
public interface IMySignalRHub : ITransientDependency
{
Task SendMessage(string message);
}
public class MySignalRHub: AbpCommonHub, IMySignalRHub {
protected IHubContext<MySignalRHub> _context;
protected IOnlineClientManager onlineClientManager;
protected IClientInfoProvider clientInfoProvider;
public MySignalRHub(
IHubContext<MySignalRHub> context,
IOnlineClientManager onlineClientManager,
IClientInfoProvider clientInfoProvider)
: base(onlineClientManager, clientInfoProvider) {
AbpSession = NullAbpSession.Instance;
_context = context;
} …Run Code Online (Sandbox Code Playgroud)c# typescript asp.net-core aspnetboilerplate asp.net-core-signalr
我正在使用带有 ASP.NET Core 2.1 的 signalR 来发送和接收来自服务器到客户端或客户端到服务器的消息,即使我还使用流通道从客户端到服务器发送大于 30kb 的块的长消息。
但是现在我想知道是否可以使用语音或视频在服务器和客户端之间进行通信?
我检查了 WebRTC 是否为我的要求提供了解决方案,但由于我已经在使用 SignalR,所以我正在寻找使用 SignalR 进行 websocket 传输的解决方案。
我对SignalR. 显然,有什么称呼它如一些争论onClosed(),closed()等等。
在SignalR客户端的侦听器中,我正在尝试实现此事件,但不断收到错误消息,指出它不是函数。我试过onClosed()和closed()。同样的错误。如何检测客户端的关闭事件?
const signalRListener = () => {
connection.on('new_message', message => {
// Handle incoming message.
// This is working fine.
})
connection.closed(e => {
// Try to restart connection but I never get here to due error I'm receiving
})
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
这是我开始连接的方式:
export const signalRStart = (token) => {
connection = new signalR.HubConnectionBuilder()
.withUrl("/chat?access_token=" + token)
.configureLogging(signalR.LogLevel.Information)
.build();
// Set connection time out
connection.serverTimeoutInMilliseconds = …Run Code Online (Sandbox Code Playgroud) 我正在使用来自此 npm 包的 @aspnet/signalr 官方 Javascript 客户端
我想知道是否有办法将Header配置添加到客户端连接头
我如何建立联系
let connection = new signalR.HubConnectionBuilder()
.withUrl(
"https://some.signalr-host.com"
)
.build();
我正在尝试了解有关在 Web 套接字服务出现故障时将 Websocket 连接服务器重新连接到服务器的更多信息。我已经做了很多环顾文档和其他问题(主要是查找客户端到服务器),但无法决定我要实现的内容。
所以目标是当服务 A 启动时,我让微服务 A 连接到微服务 B 上的 Web 套接字。一切正常,但是当我关闭服务 B 时,当我重新启动 B 时,服务 A 中的 HubConnection 状态总是断开连接。所以例如使用Microsoft.AspNetCore.SignalR.Client 1.1.0
public class MessageHubProxy : IMessageHubProxy
{
private readonly HubConnection _hubConnection;
public MessageHubProxy()
{
_hubConnection = new HubConnectionBuilder().WithUrl("http://localhost:54994/messageHub").Build();
InitiateConenction().Wait();
}
private async Task InitiateConenction()
{
await _hubConnection.StartAsync();
}
public void AddMessage(string message)
{
_hubConnection.InvokeAsync("addMessage", post);
}
}
Run Code Online (Sandbox Code Playgroud)
当我在_hubConnection停止并启动服务 B 并调用 AddMessage 后查看 AddMessage 时。我在 .. 上看到以下属性HubConnection:
HandshakeTimeout: 15 seconds,
KeepAliveInterval: 15 seconds,
ServerTimeout: …Run Code Online (Sandbox Code Playgroud) websocket signalr signalr.client asp.net-core asp.net-core-signalr
我想在服务中注入我的强类型集线器,但我不喜欢 Microsoft 所示示例中的某些内容 - https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view =aspnetcore-2.2(注入强类型 HubContext)
public class ChatController : Controller
{
public IHubContext<ChatHub, IChatClient> _strongChatHubContext { get; }
public ChatController(IHubContext<ChatHub, IChatClient> chatHubContext)
{
_strongChatHubContext = chatHubContext;
}
public async Task SendMessage(string message)
{
await _strongChatHubContext.Clients.All.ReceiveMessage(message);
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子ChatHub中耦合到ChatController.
所以我想注入用通用接口参数定义的集线器本身,并且不会在我的服务中定义它的具体实现。这是示例代码
public interface IReportProcessingClient
{
Task SendReportInfo(ReportProgressModel report);
}
Run Code Online (Sandbox Code Playgroud)
public class ReportProcessingHub : Hub<IReportProcessingClient>
{
public async Task SendMessage(ReportProgressModel report)
{
await Clients.All.SendReportInfo(report);
}
}
Run Code Online (Sandbox Code Playgroud)
public class ReportInfoHostedService : IHostedService, IDisposable
{
private readonly Hub<IReportProcessingClient> …Run Code Online (Sandbox Code Playgroud) signalr signalr-hub .net-core asp.net-core asp.net-core-signalr
链接到 SDK 程序集并构建项目时遇到此异常:
Java.Interop.Tools.Diagnostics.XamarinAndroidException: error XA2006: Could not resolve reference to 'Microsoft.AspNetCore.Http.Features.Authentication.IAuthenticationHandler' (defined in assembly 'Microsoft.AspNetCore.Http, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60') with scope 'Microsoft.AspNetCore.Http.Features, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. When the scope is different from the defining assembly, it usually means that the type is forwarded.
我添加了 Signalr.Client v. 3.1.2 以在 Xamarin 中使用 Signalr。
这是 PCL csproj:
<PackageReference Include="Fody" Version="6.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Forms.Controls.FlexButton" Version="0.11.0" />
<PackageReference Include="Microsoft.AppCenter" Version="3.0.0" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="3.0.0" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="3.0.0" /> …Run Code Online (Sandbox Code Playgroud) 我是使用 SignalR 的新手,并且在尝试将 IHubContext 注入类中时遇到了麻烦。当我尝试运行代码时,出现如下所示的异常。我尝试在startup.cs中注入TimerEventHandler时出现错误,代码如下:
public class TimerEventHandler : ITimerEventHandler
{
private readonly IConfiguration _config;
private readonly IHubContext<IndexHub> _hubContext;
public TimerEventHandler(IHubContext<IndexHub> hubContext,
IConfiguration config)
{
_hubContext = hubContext;
_config = config;
}
}
Run Code Online (Sandbox Code Playgroud)
ITimer事件处理器
public interface ITimerEventHandler
{
Task OnTimedEvent(object source, ElapsedEventArgs e, string connectionId);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 Startup.cs 中的代码
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR(o =>
{
o.EnableDetailedErrors = true;
});
services.AddScoped<ITimerEventHandler, TimerEventHandler>();
}
Run Code Online (Sandbox Code Playgroud)
代码来自indexhub
public class IndexHub : Hub
{
private readonly IServiceBusHelper _serviceBusHelper;
private readonly ITimerEventHandler _timerEventHandler;
public …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 Ocelot API 网关将 Blazor 客户端连接到简单微服务中的 SignalR 集线器。我对所有 ASP.NET Core 项目都使用 SSL。
网关在调用 https 端点时工作正常,当我直接从网关浏览器调用 signalR hub 端点(正确显示 Ocelot 路由)时,我收到“需要连接 ID”。
不幸的是,当我尝试从 blazor 客户端应用程序连接到集线器时,出现以下错误
失败:Ocelot.Errors.Middleware.ExceptionHandlerMiddleware[0] requestId:0HM4U0GLR9ACR:00000001,previousRequestId:没有以前的请求ID,消息:全局错误处理程序中捕获异常,异常消息:仅以“ws://”或“wss”开头的Uris支持 ://'。(参数'uri'),异常堆栈:在Ocelot.WebSockets.Middleware.WebSocketsProxyMiddleware.Proxy(HttpContext context,String serverEndpoint)在Ocelot.WebSockets.Middleware的System.Net.WebSockets.ClientWebSocket.ConnectAsync(Uri uri,CancellationToken CancellationToken) .WebSocketsProxyMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) 在 Ocelot.DownstreamUrlCreator.Middleware.DownstreamUrlCreatorMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)在 Ocelot.LoadBalancer.Middleware.LoadBalancingMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) 在 Ocelot.Request.Middleware.DownstreamRequestInitialiserMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware .Invoke(HttpContext httpContext) 在 Ocelot.Multiplexer.MultiplexingMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) 在 Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)
以下是我的代码。
Ocelot API 启动文件
public class Startup
{
public void ConfigureServices(IServiceCollection …Run Code Online (Sandbox Code Playgroud) microservices asp.net-core asp.net-core-signalr ocelot blazor-client-side
asp.net-core ×8
signalr ×7
c# ×3
signalr-hub ×2
.net-core ×1
ocelot ×1
typescript ×1
websocket ×1