简体   繁体   中英

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

I want to process a stream of data and need to display the processed data near real time. For that 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 which processes the data is,

     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 side code is

    <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 ProcessEvents method in the Hub class and that registers the SimpleEventProcessor. And thus execution comes into ProcessEventsAsync in SimpleEventProcessor. From this ProcessEventsAsync method, I need to call the clientside code to display the data. Do I need to make SimpleEventProcessor also as a hub class?

You can use following code to get hold of HubContext which allows you to invoke client methods from outside of hub istance:

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

Here I am facing a problem that The client method DisplayMessage is not executing everytime. I need to display a stream of message. But when I put debugger in the client code, it executes everytime.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 a stream of messages? Why it is only working with debugger?

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