SignalR calls a server method, and that method calls a callback method outside of the hub. How can I call the client function from this method?

I want to process a data stream and display the processed data in real time. For this I created a hub class

    public class AzureGuidanceEventHubReceiver : Hub
      {
        EventProcessorHost eventProcessorHost;
        public async void ProcessEvents()
         {
           //Do some code here
           eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();
         }
      }

      

And the class that processes the data,

     public class SimpleEventProcessor : IEventProcessor
      {

        public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> events)
           {
            foreach (EventData eventData in events)
             {
                int data;
                var newData = this.DeserializeEventData(eventData);
                //how to display newData in the browser????????????????????????????????                                                
             }
     }

      

My client code                                   

    <script type="text/javascript">
    $(function () {
        var receiverHub= $.connection.azureGuidanceEventHubReceiver;
        receiverHub.client.displayMessage = function (data) {
        var encodedData = $('<div />').text(data).html();
        // Add the message to the page. 
            $('#discussion').append('<li><strong>' + encodedData + '</li>');
        };

        //// Start the connection.
        $.connection.hub.start().done(function () {

                $('#sendmessage').click(function () {
                receiverHub.server.processEvents();
            });
        });
    });

      

Here I made a call to the ProcessEvents method in the Hub class and registered SimpleEventProcessor. And thus the execution happens in ProcessEventsAsync in SimpleEventProcessor. From this ProcessEventsAsync method, I need to call the client code to display the data. Do I need to make SimpleEventProcessor also like a hub class?

+3


source to share


2 answers


You can use the following code to get the HubContext, which allows you to call client methods from outside the hub istance:



var hubContext =  GlobalHost.ConnectionManager.GetHubContext<AzureGuidanceEventHubReceiver>();
hubContext.Clients.All.displayMessage(dataToDisplay);

      

+2


source


I ran into the problem here that the client's DisplayMessage method is not executed every time. I need to display a message flow. But when I add a debugger to the client code, it gets executed every time. Here is my client code.

    chat.client.displayMessage = function (data) {                
            // Html encode display data 

            debugger;

            var encodedData = $('<div />').text(data.GPSPosition).html();
            var data = encodedData.split("\n");
            var varlat = data[0].replace("Latitude:","").trim();
            var varlong = data[1].replace("Longitude:", "").trim();
            ShowInGoogleMap(varlat, varlong);
          };

      



how can i display the message flow? Why does it only work with the debugger?

+1


source







All Articles