在ASP.NET Core SignalR中,如何从服务器向客户端发送消息?

时间:2018-06-10 21:13:27

标签: c# asp.net-core asp.net-core-signalr

我使用新发布的ASP.NET Core 2.1成功设置了SignalR服务器和客户端。我通过ChatHub扩展Hub建立了一个聊天室:每当有来自客户端的消息时,服务器都会通过Clients.Others将其撤回。

我还不了解的是如何向客户端发送消息而不是作为对传入消息的响应。如果服务器正在工作并产生结果,我如何获得Hub的访问权限以便向特定客户端发送消息? (或者我甚至需要访问Hub?还有其他方式发送消息吗?)

搜索此问题很困难,因为大多数结果来自旧版本的ASP.NET和SignalR。

3 个答案:

答案 0 :(得分:7)

您可以将Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support.constraint:constraint-layout:1.1.0. Open File Show Details Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve com.android.support.test:runner:1.0.2. Open File Show Details Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve com.android.support.test.espresso:espresso-core:3.0.2. Open File Show Details Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve com.android.support.constraint:constraint-layout:1.1.0. Open File Show Details Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve com.android.support.constraint:constraint-layout:1.1.0. Open File Show Details Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve com.android.support.constraint:constraint-layout:1.1.0. Open File Show Details Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve com.android.support.constraint:constraint-layout:1.1.0. Open File Show Details Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0. Open File Show Details 类注入服务并使用该服务调用客户端。

IHubContext<T>

现在您可以将public class NotifyService { private readonly IHubContext<ChatHub> _hub; public NotifyService(IHubContext<ChatHub> hub) { _hub = hub; } public Task SendNotificationAsync(string message) { return _hub.Clients.All.InvokeAsync("ReceiveMessage", message); } } 注入您的班级并向所有客户发送消息:

NotifyService

答案 1 :(得分:1)

将hubcontext简单地插入到使用hubcontext的类中。

你会在那里找到详细信息:

Call SignalR Core Hub method from Controller

答案 2 :(得分:1)

现在有SignalR HubContext的官方Microsoft文档可以回答您的问题 https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.1

但是,正如其他人所指出的那样,您需要通过依赖注入获取IHubContext的实例,以访问集线器之外的集线器方法。