Very Simple Network Example

I have received a couple requests for an example of networking, so I put something very basic together to show some simple functionality. 

What it does have:

  • Functionality to download a file from a server to persistent storage
  • Calculation of percentage of download complete

What it does not have:

  • Server component - This will work in a development environment, but not elsewhere. You will still need to take care of AACS encapsulation and server certificates for this to function in the real world
  • Management of connections - You may have up to 8 active HTTP clients at one time. This script does not check for that
  • P-storage storage capacity check - I am assuming there is sufficient space in persistent storage to download this file. That is a bad real world assumption.
  • Parsing of XML from a server - Your initial server communication will most likely be to request an XML string that describes the downloads available. This XML should also contain the file size of your downloads so you can check p-storage for capacity and show download progress
  • Lots of other important features like using headers, using other request methods, sending files to a server, dynamically generating a timeout value, handling failed downloads when not using a timeout, etc.

//location of file to be downloaded

//the real URL should be a valid domain

var src = "https://localhost/sample_file.txt";

//destination where download will be stored

//here its the same file name as the downloaded file, stored in p-storage

var dest = "file:///required/" + PersistentStorageManager.contentId + src.substring(src.lastIndexOf("/"),src.length);

//timeout in seconds...use -1 for no timeout

var timeout = 30;

//size of download - use for percent download complete

var downloadSize = 5000;

//network object - referenced in OnDownloadStateChange

var httpClient;

//confirm user has network connection

if (Player.capabilities.network.connected == true)

{

    try

    {

  httpClient = Network.createHTTPClient(src, Network.HTTP_GET, timeout);

        httpClient.downloadFileLocation = dest;

       

        //set a callback to track progress of download

        httpClient.onStateChange = OnDownloadStateChange;

        httpClient.send();

    }

    catch(ex)

    {

      TraceError("Download FAILED", ex, arguments.callee);

    }

}

else

{

    TraceInfo("Network connection not available",arguments.callee);

}

function OnDownloadStateChange(state)

{

  switch (state)

  {

    case httpClient.STATE_RESPONSEPROGRESS:

        /*

            Download is still occuring

            If you know your download size, you can look at

            httpClient.dataDownloaded to determine percent progress

        */

        if (downloadSize)

        {

            var percentComplete = Math.floor(100 * httpClient.dataDownloaded / downloadSize);

        }

        break;

    case httpClient.STATE_COMPLETED:

        /*

            Do something with file that is now at downloadLocation

        */

        TraceInfo("STATE_COMPLETED",arguments.callee);

        httpClient = null; //destroy client to avoid closure issue

        break;

    case httpClient.STATE_ERROR:

        /*

            Download failed, handle accordingly

        */

     TraceInfo("STATE_ERROR",arguments.callee);

        httpClient = null; //destroy client to avoid closure issue

        break;

    case httpClient.STATE_ABORT:

        /*

            Download aborted, handle accordingly

        */

        TraceInfo("STATE_ABORT",arguments.callee);

        httpClient = null; //destroy client to avoid closure issue

        break;

  }

}

 

UPDATE:  

Or, alternatively if you are only expecting a text string as a response from the server, don't set downloadFileLocation and on STATE_COMPLETED, use httpClient.getResponseString() to get that text string