user1037552
user1037552

Reputation: 269

How to read Live streaming data using AJAX

I want to read live streaming data from my URL(ie.http://..) My URL(ie.http://..) contain numeric data and it's continuously growing. i want to read that data in to my file(HTML5 & javascript). I have done with static numeric data using AJAX. But while duing it with dynamic data(live streaming data). i am not able to get responseText(). Is it possible to take responseText() of that URL(ie.http://..) which contain live streaming data? how i can do this? My code for reading static data is

<!DOCTYPE HTML>
<html>
<head>    

<script type="text/javascript">
  function accessWebservice()
    {           
        var xmlhttp;
        xmlhttp = new XMLHttpRequest();

 //xmlhttp.open("get","http://192.168.15.174/Streamer/StartStream.aspx?IsTestData=true",true);   
 //above URL contains live streaming numberic data that i want to read
 //But when i am using above URL i am not getting responseText (**How to get it?**)
  xmlhttp.open("get","http://localhost/StaticDemoData.txt",true);   //This contains static data
        xmlhttp.onreadystatechange=function() {
         if (xmlhttp.readyState==4)
         {
             if (xmlhttp.status == 200 )
              {
                 var responseData=xmlhttp.responseText;
           alert(responseData);
              }
             else
             {
                 alert("Server returned: " + xmlhttp.status);
             }
         }
        }

        xmlhttp.send(null);
    }
</script>
</head>

How to get 'xmlhttp.responseText' for live streaming numeric data?

Upvotes: 4

Views: 4000

Answers (2)

Smith
Smith

Reputation: 103

Try this script to get stream your data... but you need jquery.js file in your directory and StaticDemoData.txt you can change with other file in .php extension and get your query on the file

<html>
<head>
<script type="text/javascript" src="jquery-1.5.1.js"></script>

<script type="text/javascript">
$(document).ready(function(){
  userdetails();
});


function userdetails(){
     $.post('StaticDemoData.txt',function(data){
         $('.result').html(data);
       });     

       setTimeout("userdetails()",1000);
}

</script>
</head>
<body>
<div class="result"></div>
</body>
</html>

Upvotes: 1

Arnavion
Arnavion

Reputation: 3968

If you check for xmlhttp.readyState == 3 (XMLHttpRequest.LOADING), then accessing xmlhttp.responseText will give you the data that has been received from your server so far. You can then use a setInterval to constantly check xmlhttp.responseText for new data.

Upvotes: 2

Related Questions