简体   繁体   中英

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).

Now, I want to use SignalR to monitor & receive the SubscriptionClient messages...and I have test-code that works just fine on its' own.

I have found many examples for sending messages to an Azure Queue (that is easy). And, I have the code to receive a BrokeredMessage from a SubscriptionClient . However, I cannot get SignalR to continuously monitor my Distribute method.

How do I get SignalR to monitor the 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. 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. 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.

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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