简体   繁体   English

如何从SignalR集线器接收主题消息?

[英]How do I receive Topic messages from a SignalR hub?

I have successfully created an Azure application that sends DbTransactions to a ServiceBus Queue , and then, enqueues a 'notifying message' to a ServiceBus Topic for other clients to monitor (...so they can receive the updates automatically). 我已经成功创建了一个Azure应用程序,该应用程序将DbTransactions发送到ServiceBus Queue ,然后将“通知消息”加入ServiceBus Topic以供其他客户端监视(...以便他们可以自动接收更新)。

Now, I want to use SignalR to monitor & receive the SubscriptionClient messages...and I have test-code that works just fine on its' own. 现在,我想使用SignalR监视和接收SubscriptionClient消息...,并且我有可以正常工作的测试代码。

I have found many examples for sending messages to an Azure Queue (that is easy). 我发现了许多将消息发送到Azure队列的示例(这很容易)。 And, I have the code to receive a BrokeredMessage from a SubscriptionClient . 并且,我有从SubscriptionClient接收BrokeredMessage的代码。 However, I cannot get SignalR to continuously monitor my Distribute method. 但是,我无法让SignalR持续监视我的Distribute方法。

How do I get SignalR to monitor the Topic ? 如何获得SignalR来监视Topic

CODE BEHIND: (updated) 后面的代码:(已更新)

    public void Dequeue()
    {
        SubscriptionClient subscription = GetTopicSubscriptionClient(TOPIC_NAME, SUBSCRIPTION_NAME);

        subscription.Receive();

        BrokeredMessage message = subscription.Receive();

        if (message != null)
        {
            try
            {
                var body = message.GetBody<string>();
                var contextXml = message.Properties[PROPERTIES_CONTEXT_XML].ToString();
                var transaction = message.Properties[PROPERTIES_TRANSACTION_TYPE].ToString();

                Console.WriteLine("Body: " + body);
                Console.WriteLine("MessageID: " + message.MessageId);
                Console.WriteLine("Custom Property [Transaction]: " + transaction);

                var context = XmlSerializer.Deserialize<Person>(contextXml);

                message.Complete();

                Clients.All.distribute(context, transaction);
            }
            catch (Exception ex)
            {
                // Manage later
            }
        }
    }

CLIENT-SIDE CODE: 客户端代码:

    // TEST: Hub - GridUpdaterHub
    var hubConnection = $.hubConnection();
    var gridUpdaterHubProxy = hubConnection.createHubProxy('gridUpdaterHub');

    gridUpdaterHubProxy.on('hello', function (message) {
        console.log(message);
    });

    // I want this automated
    gridUpdaterHubProxy.on('distribute', function (context, transaction) {
        console.log('It is working');
    });

    connection.start().done(function () {

        // This is successful
        gridUpdaterHubProxy.invoke('hello', "Hello");
    });

I would not do it like that. 我不会那样做。 Your code is consuming and retaining ASP.NET thread pool's threads for each incoming connection, so if you have many clients you are not scaling well at all. 您的代码占用并为每个传入连接保留ASP.NET线程池的线程,因此,如果您有许多客户端,那么扩展性就不好。 I do not know the internals of SignalR that deep, but I'd guess that your never-ending method is preventing SignalR to let the client call your callbacks because that needs the server method to end properly. 我不十分了解SignalR的内部原理,但我想您永无止境的方法会阻止SignalR让客户端调用您的回调,因为这需要服务器方法正确结束。 Just try to change while(true) with something exiting after, let's say, 3 messages in the queue, you should be called back 3 times and probably those calls will happen all together when your method exits. 只需尝试更改while(true)并在队列中有3条消息之后退出,就应该回调3次,并且可能在方法退出时这些调用会一起发生。

If that is right, then you can move to something different, like dedicating a specific thread to consuming the queue and having callbacks called from there usning GlobalHost.ConnectionManager.GetHubContext . 如果这是正确的,那么你就可以移动到不同的东西,比如专特定线程使用了该队列,并有回调从那里usning称为GlobalHost.ConnectionManager.GetHubContext Probably better, you could try a different process consuming the queue and doing HTTP POST to your web app, which in turns broadcasts to the clients. 可能更好,您可以尝试使用其他过程来消耗队列并对您的Web应用程序执行HTTP POST ,这反过来又广播给客户端。

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

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