Question

The Problem

I'm using the following code to constantly keep my UI accurate. I dislike the slight one second delay though. It currently only returns a small JSON file with only a few keys about updates to the UI and other important information for the page.

My Script This Far:

function parseDynamicData(ard) {
    if (ard['details']['enabled'] == 'true' && ard['song']['art'] != 'undefined');
    {
        if (document.location.toString().indexOf('#offline') != -1)
            document.location = '/#tracks';
        $('#track-title').html(htmlDecode(ard['song']['title']));
        $('#track-artist').html(htmlDecode(ard['song']['artist']));
        $('#track-album').html(htmlDecode(ard['song']['album']));
        $('#track-art').attr('src', htmlDecode(ard['song']['art']));
        if (htmlDecode(ard['details']['playing']) == 'true') {
            $('#control-pauseplay').html('Pause');
            $('#control-pauseplay').attr('href', '/track?proc=2');
        } else {
            $('#control-pauseplay').html('Play');
            $('#control-pauseplay').attr('href', '/track?proc=3');
        }
    }
}

function updateLoop() {
    $.ajax({
        url: '/jtrack',
        dataType: 'json',
        cache: false,
        async: false,
        success: parseDynamicData,
        error: function (xhr) {
            document.location = '#offline';
        },
        complete: function (x, y) {
            setTimeout(updateLoop, 3000);
        }
    });
}

$(document).ready(function () {
    setTimeout(updateLoop, 1500);
});

My Question

How fast can I safely make these requests? I'm running this on mobile devices (such as the iPhone or Android) so I assume I need to not do it all too often. Is their a better way? If so, how can I achieve this with a .net HttpListener server?

The Major Caveat:

This is all part of a project that I'm doing. The problem is that the server this is connecting to is a HttpServer I've written in VB.net. I'm not the most proficient person with this but this server is specifically designed for my project (no other solutions really suffice in this area because the Http Server is one of the main aspects of the application/project itself).

So Keep in Mind:

This is a local server that is hosted by your own computer. Only one or two devices should ever really be connected at one time (connected via WiFi to the LAN of the host).

Oh and...

For all you "sad pandas" ( @Dave Newton :P ) worried about getting their data usage maxed out...don't worry. This will only be used when your mobile device is connected via WiFi. It won't even work otherwise because it has to be connected to the LAN of the person running the application (Unless somebody port-forwarded it, but I see no reason anybody would want to do that).

Was it helpful?

Solution

As per the comments, going for websockets, comet or your own hand-rolled long polling solution will be best.

Rapid fire requests are a Bad Thing (tm)

What you are aiming for is keeping a single connection open for as long as possible waiting for data from the server. If the connection drops you start the next request and wait again.

Seeing as you're using .NET, I would highly recommend looking at SignalR for managing your communication channel. It supports many different hosting options (ASP.NET, self-hosted in your own appdomain, etc.) so should be highly suitable.

However, you say that writing the server yourself is key to this project. I assume this an academic exercise - if not, you're most likely re-inventing the wheel. Go and look at how SignalR implements maintaining an open communication channel to a client. The point here is that you do NOT want to be making frequent requests to the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top