2
\$\begingroup\$

I want to create a web app that should let users remotely make a device do a variety of things simply using any other device, anywhere, without installing anything. To do that, the device which performs tasks should receive commands as fast as possible, and, possibly, using few Internet data.

This is the code I'm going to use to receive the commands. Obviously, a login system still needs to be implemented.

index.php (the device which receives commands must keep it opened in the web browser)

<?php
$lastModified=filemtime(__FILE__);
$etagFile = md5_file(__FILE__);
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
header("Etag: $etagFile");
header('Cache-Control: public');
if (strtotime($ifModifiedSince)==$lastModified || $etagHeader == $etagFile){
    header("HTTP/1.1 304 Not Modified");
    exit;
}
else{
?>
<!DOCTYPE html>
<html>
<head>
<script>
function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]
 if (window.ActiveXObject){
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
   }
  }
 }
 else if (window.XMLHttpRequest)
  return new XMLHttpRequest()
 else
  return false
}
</script>
<style>
body{
    font-family:Arial,Verdana,Geneva;
}
</style>
<title>Remote control</title>
</head>
<body>
<div id="content">
Hey there! It seems that you turned Javascript off. Turn it back on in order to make this site work.
</div>
<script>
function carica(){
    document.getElementById("content").innerHTML="I\'m ready to receive remote commands"
    var request=new ajaxRequest()
    request.onreadystatechange=function(){
     if (request.readyState==4){
      if (request.status==200 || window.location.href.indexOf("http")==-1){
       document.getElementById("content").innerHTML=request.responseText
      }
      else if(request.status==204){
          carica()
      }
      else{
       document.getElementById("content").innerHTML='An error occurred!';
      }
     }
    }
    request.open("GET", "content.php", true)
    request.send()
}
carica();
</script>
</body>
</html> 
<?php
}
?>

content.php

<?php
$maximumAttempts = 30;
$sleepTime = 10;
$totalTime = $maximumAttempts * $sleepTime;
ini_set('max_execution_time', $totalTime);
for($i=1;$i<$maximumAttempts;$i++){
    if(file_exists("command.txt")){ //a different check will be performed once the login system will be implemented
        echo 'New command for this device received!'; //commands still need to be implemented
        $commandReceived = 1;
        break;
    }
    else{
        sleep($sleepTime);
    }
}
$commandReceived = isset($commandReceived) ? $commandReceived : 0;
if($commandReceived!=1){
    http_response_code(204);
}
?>

Is this method to receive commands good? Does it overload the server? Is it fast enough for the client?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

it's a bit hard to comment on your question as it's really very general. You say that the device should receive commands as fast as possible and with very few data and than you ask "Does it overload the server? Is it fast enough for the client?", but you don't mention what kind of operations would be actually performed and in what way. The process of receiving and parsing command is usually much faster than the process of executing parsed command.

As for the code itself, the client side looks a bit like reinventing the wheel - why don't you try jQuery? As for the server code (content.php), it's hard to tell what it does other than it obviously tries to read a file from disk serveral times, but even if succeeds - it still does nothing...

Maybe if you'd ask more specific questions, I could be more of a help.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your answer. I've not written the executing part yet, I've just concentrated on the reception of commands and I was wondering if it was OK before continuing, I'm posting a more specific question when I'll have finished the project, thanks again \$\endgroup\$
    – Stubborn
    Jun 19, 2015 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.