I am running the following code...
$http.get(cmdUrl, {async: true, cache: false}).
success(function(data, status, headers, config) {
$scope.logText = data;
}).
error(function(data, status, headers, config) {
$scope.logText = "An error occurred";
});
When I run my application, I have a button that when clicked will invoke the get() method above. The first time the button is clicked, the success callback runs successfully. My problem is that when I click the button a second time I get no response. I stepped through the code with a (Chrome) debugger and I see that I get a status code of 200 after the first invocation but on every subsequent call, the request is in a pending state. Furthermore, while stepping through the code, on each subsequent request neither the success or the error callback are executed. Also, after a while, I guess the pending requests time out and in the console, I see a ERR_CONNECTION_REFUSED for each of requests that were pending.
I want to know why the subsequent get's are put in pending state and how can I get them to execute to completion.
Thanx!!!
This is the code on the server side of things written using NodeJS...
app.get('/cmdURL', function(req, resp, next) {
var intercept = require('intercept-stdout');
var deasync = require('deasync');
var captured_text = "";
var unhook_intercept = intercept(function(txt) {
captured_text += txt;
});
var logs = [];
var responseText = "";
var done = false;
var hook_stream = function(_stream, fn) {
// Reference default write method
var old_write = _stream.write;
// _stream now write with our shiny function
_stream.write = fn;
return function() {
// Reset to the default write method
_stream.write = old_write;
};
};
// Set hook for standard output
var unhook_stdout = hook_stream(process.stdout, function(string, encoding, fd) {
if (string.includes("-- end of response --")) done = true;
logs.push(string);
});
var command = req.query.text;
if (command) {
var valid = issueCommand(command);
if (valid) {
while(!done) {
deasync.sleep(200);
}
unhook_stdout();
console.log('Not hooked anymore.');
// Now do what you want with logs stored by the hook
logs.forEach(function(_log) {
responseText += _log;
});
}
else {
unhook_stdout();
responseText += "Unrecognized command";
}
resp.status(200).send(responseText);
}
else {
console.log('No command was specified.');
resp.status(404).send('No command was specified.');
}
});
function issueCommand(cmd) {
// Issue an asynchronous cmd
}