Hello!

You mustn’t use standard sleep function for it will block Nginx’s events
loop, alternatively, you need to put your write event to a timer, set the
proper handler when the timer expires.
BTW, you should always check the return value of ngx_http_send_header and
ngx_http_output_filter.


On 10 July 2017 at 01:43:46, Johan Andersson (n...@firemail.cc) wrote:

Hi everyone,

I have some issues writing my nginx modules.

I am on Debian Stretch, installed nginx with the default configuration,
and took the hello_world module. It works without a hitch. Then I
changed the handler to send three "hello world" responses, and sleep for
one second between each response.

However, when I look at the result in my browser, the page loads, pauses
for three seconds, and then displays all three "hello world" messages at
once.

Actually I was flushing each response, so I expected each "hello world"
message to appear one after the other, with one second pause between
them.

Am I doing something wrong? Is this event the correct way to achieve
this? All functions return NGX_OK. This is my code:

static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;
ngx_int_t result;

r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;
//r->headers_out.content_length_n = sizeof(ngx_hello_world);
ngx_http_send_header(r);

for(int i = 0; i < 3; i++)
{
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

out.buf = b;
out.next = NULL;

b->pos = ngx_hello_world;
b->last = ngx_hello_world + sizeof(ngx_hello_world);
b->memory = 1;
b->flush = 1;
b->last_buf = (i == 2);

result = ngx_http_output_filter(r, &out);
ngx_http_send_special(r, NGX_HTTP_FLUSH);

sleep(1);
}

return result;
}

Cheers
Johann
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx

Reply via email to