简体   繁体   English

SignalR自托管Windows服务,监听消息

[英]SignalR self hosted windows service, listening for messages

I'm attempting to build a windows service that's self-hosting SignalR. 我正在尝试构建一个自托管SignalR的Windows服务。

I have read through tutorials such as SignalR Self-Host on ASP.Net 我已经阅读了ASP.Net上的SignalR Self-Host等教程

I'm noticing that, least it seems, that they're based around broadcasting messages, and can't seem to find anything around listening. 我注意到,至少看起来,它们是基于广播信息,并且似乎无法找到任何有关聆听的内容。

I need to listen to messages from within the service, as well as broadcast. 我需要收听服务中的消息以及广播。

We already have our backplane setup - it's the same one that the site uses. 我们已经有了背板设置 - 它与网站使用的设置相同。

In a web site I can join a group, via Javascript. 在一个网站上,我可以通过Javascript加入一个群组。 How do I join a group in a self-hosted SignalR service. 如何在自托管SignalR服务中加入组。

In a web site I register a callback on a hub. 在网站中,我在集线器上注册回调。 How do i register the same callback in a self-hosted service? 如何在自托管服务中注册相同的回调?

some example code that I have in place, for registering and starting SignalR is: 我有一些示例代码,用于注册和启动SignalR:

GlobalHost.DependencyResolver.UseSqlServer(Settings.Default.ISDBContext);
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            HubConfiguration hubConfig = new HubConfiguration()
            {
                EnableDetailedErrors = true,
                EnableJSONP = true,
            };

            map.RunSignalR(hubConfig);

        });

I then start my webApp like this: 然后我像这样启动我的webApp:

SignalR = WebApp.Start<Startup>(options);

options are the url's i'm registering. 选项是我正在注册的网址。 Startup is the startup class containing the signalR mapping above. Startup是包含上面signalR映射的启动类。

Little lost here as I haven't built a self-hosting service before 由于我之前没有建立自助托管服务,因此很少丢失

In general SignalR offers a real-time messaging environment with the benefit that it can push messages to clients without them requesting the update (you've read the intro, so enough for that). 通常,SignalR提供了一个实时的消息传递环境,它可以将消息推送到客户端,而无需他们请求更新(您已经阅读了介绍,因此足够了)。 Whether you start your self-host from within a service shouldn't make a difference. 无论您是从服务中启动自托管,都不应该有所作为。

From what I understand from your scenario: You need to consume the messages from the self-hosted service. 根据我对您的场景的理解:您需要使用来自自托管服务的消息。 I think you might only need a hint to the SignalR Desktop Client . 我想你可能只需要一个SignalR Desktop Client的提示。 I think your app/service should start off the service and then act as a client to the service itself. 我认为您的应用/服务应该从服务开始,然后充当服务本身的客户端。 This would be the cleanest approach. 这将是最干净的方法。

As the javascript client it acts as a consumer of the service with the same capabilities: 作为javascript客户端,它充当具有相同功能的服务的使用者:

HubConnection conn = new HubConnection("http://192.168.1.1:8080");
IHubProxy hub = conn.CreateHubProxy("ChatHub");

// call from hub to client
hub.On<string, string>("addMessage", (name, message) =>
{
     // Handle incoming data update
});

and vice versa from the desktop client to the hub: 从桌面客户端到集线器,反之亦然:

await hub.Invoke<string, string>("Send", name, message);

To work with groups, the logic needs to be defined within your hub. 要使用组,需要在集线器中定义逻辑。 The documentation Working with groups gives an understandable overview. 文档使用组提供了可理解的概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM