locked
How to call method using jquery in mvc razor page at 10 sec interval and append output to html RRS feed

  • Question

  • below code read messages from iot hub one by one as it comes. 
     

          private async void MonitorEventHubAsync(DateTime startTime, CancellationToken ct, string consumerGroupName)
            {
                EventHubClient eventHubClient = null;
                EventHubReceiver eventHubReceiver = null;

                try
                {
                    string mesageData = string.Empty;
                    int eventHubPartitionsCount;

                    string selectedDevice = "";
                    eventHubClient = EventHubClient.CreateFromConnectionString("activeIoTHubConnectionString", "messages/events");
                    mesageData = "Receiving events...\r\n";
                    eventHubPartitionsCount = eventHubClient.GetRuntimeInformation().PartitionCount;
                    string partition = EventHubPartitionKeyResolver.ResolveToPartition(selectedDevice, eventHubPartitionsCount);
                    eventHubReceiver = eventHubClient.GetConsumerGroup(consumerGroupName).CreateReceiver(partition, startTime);

                    //receive the events from startTime until current time in a single call and process them
                    while (true)
                    {
                        var eventData = eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(1)).Result;

                        if (eventData != null)
                        {
                            var data = Encoding.UTF8.GetString(eventData.GetBytes());
                            var enqueuedTime = eventData.EnqueuedTimeUtc.ToLocalTime();
                            var connectionDeviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString();

                            if (string.CompareOrdinal(selectedDevice.ToUpper(), connectionDeviceId.ToUpper()) == 0)
                            {
                                mesageData += $"{enqueuedTime}> Device: [{connectionDeviceId}], Data:[{data}]";

                                if (eventData.Properties.Count > 0)
                                {
                                    mesageData += "Properties:\r\n";
                                    foreach (var property in eventData.Properties)
                                    {
                                        mesageData += $"'{property.Key}': '{property.Value}'\r\n";
                                    }
                                }

                                mesageData += "\r\n";
                            }
                        }
                    }

                }
                catch (Exception ex)
                {

                }
            }

    I want to show messages one by one on mvc cshtml page using above code how can I do that ?

    one approach i can use like below can anyone help here?

    1.In cshtml

        <p id="pValue"></p>

    2. In script

    >     var someRootPath = "@Url.Content("~")";
    >              (function randomGenerator() {
    >                 $.ajax({
    >                     url: someRootPath + 'Home/GetValue',
    >                     success: function (data) {
    >                         $('#pValue').html(data.someValue);
    >                     },
    >                     complete: function () {
    >                         setTimeout(randomGenerator, 1000);
    >                     }
    >                 });
    >             })();

    3.Controller

        [HttpGet]
        public JsonResult GetValue()
        {
            return Json( // call winform method which gives message data);
        }


      [1]: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/DeviceExplorer


    Tuesday, November 13, 2018 3:33 PM

All replies