I have a worker thread that sends out a request to the server for data using an XMLHttpRequest. That request points to a php file that basically checks the current integrity of the information that the client has and if the client needs the new information then it is sent. Otherwise the server checks information until the client needs a response. After the server responds the whole process is repeated.
The problem arises when the browser realizes the script isn't responding and gives the user the option to stop the script. As you can see, this isn't the intended result. So what's the best way to continue using the comet-like structure without confusing the browser?
EDIT: I realized why the script is hanging, I repeated the whole worker thread instead of repeating the somewhere deeper inside the thread. So I guess my question now where to start the process again, after it finishes.
<?php
//Client sends their current id
if(isset($_GET['id']))
$id = $_GET["id"];
//if id doesnt match servers id send them a new one
//other wise do not respond
$server_id = file_get_contents("ids.txt");
while($server_id == $id){
$server_id = file_get_contents("ids.txt");
sleep(1);
}
echo $server_id;
?>
Javascript:
self.addEventListener('message', function(e) {
var data = e.data;
switch (data.cmd) {
case 'start':
getInfo(data.id);
self.postMessage('ID :: ' + response);
break;
default:
self.postMessage('Unknown command');
};
}, false);
var response = null;
var request = new XMLHttpRequest();
function getInfo(inputID){
var url = "serverResponse.php?id=" + inputID;
request.open("GET", url, false);
request.onreadystatechange = updateState;
request.send(null);
}
function updateState(){
if(request.readyState == 4){
if(request.status == 200){
response = request.responseText;//getInfo(data.id);
}
}
}
html:
<html>
<script type="text/javascript">
function sayHI() {
var id = "666";
worker.postMessage({'cmd': 'start', 'id' : id});
}
var worker = new Worker('AjaxWorker.js');
worker.addEventListener('message', function(e){
document.getElementById('result').textContent = e.data;
//Some data has been received so go ahead and make another call to the server
//This is where the script hangs because the worker waits for a response
sayHI();
}, false);
</script>
<body>
<button type="button" name="submit" id="submit" onclick="sayHI()">Submit</button> </br></br>
<output id="result" name="result">Result</output>
</body>
</html>