Hi.
I'm trying to write an application using ajax, with Nginx as frontend, proxying relevant requests to a little application I wrote. All it does is to epoll some sockets and use the file descriptors. Here are some code snippets :
Nginx configuration :
upstream cometutils {
server unix:/tmp/cutil_socket;
}
...
location /test {
proxy_buffering off;
proxy_read_timeout 120;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://cometutils;
}
JS code :
var kdata="foo=bar";
$.ajax({
async:true,
url:"http://127.0.0.1/test",
data:kdata,
type:"GET",
dataType:"text",
cache:"false",
success:function(retval)
{
alert(retval);
}
});
The relevant C++ code, to give you an idea about what it does :
//fd is the file descriptor.
string resp="HTTP/1.1 200 OK\n\nContent-Type: text/plain\n\nHi there\n";
send(fd,resp.c_str(),resp.length(),0);
resp="sadsadasd\n";
sleep(2);
send(fd,resp.c_str(),resp.length(),0);
close(fd);
I'm 100% sure there's no problem with the program, it works perfectly with pretty much anything using a socket communication, including...
...regular page requests ; for instance, typing http://127.0.0.1/test?blabla=...
will make the browser show "Hi there!"
text first, then display the second message two seconds later and terminate the connection.
However, the ajax request recieves nothing at all (the response body is simply empty).
It terminates (the alert()
function is called) right after when it should've received the first message and the server-side send()
indicates that it has sent all the bytes it should, but the second send()
returns -1.
I also tried using beforeSend property and plain xmlhttprequest objects to handle onreadystatechange callback directly, but they didn't help at all (readyState never becomes 3, the response is terminated right away).
Any idea on what the reason might be is appreciated.