Re: http server push
Jan Wielemaker <[email protected]> Fri, 11 Apr 2014 12:22:16 +0200
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 04/11/2014 11:50 AM, Igor Wojnicki wrote:
> Hello,
>
> I'm trying to implement a server push based communication: a http
> connection remains open as long as possible while the client generates
> content gradually.
>
> I ran into some buffering issues. Here is a toy example:
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> :- http_handler(root(longpoll), longpoll, [time_limit(5)]).
>
> longpoll(Request) :-
> format('Content-type: text/plain~n~n'),
> format('testing~n'),
> sleep(2),
> format('testing~n').
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
It works like this:
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/thread_httpd)).
:- http_handler(root(longpoll), longpoll, [time_limit(5)]).
server :-
http_server(http_dispatch, [port(4000)]).
longpoll(_Request) :-
format('Transfer-Encoding: chunked~n'),
format('Content-type: text/plain~n~n'),
format('testing~n'),
flush_output,
sleep(2),
format('testing~n').
I.e., you need chunked transfer encoding and you need to flush.
Cheers --- Jan
>
> Expected behavior upon connecting tho /longpoll resource is:
>
> testing
> ... 2 seconds later
> testing
>
> but instead of the above I'm getting:
> ... 2 seconds delay
> testing
> testing
>
> Using flush_output/0, ttyflush/0 does not help.
>
> I guess that it has something to do with server-side buffering mentioned
> in sect. 3.1 of the http module documentation
> (http://www.swi-prolog.org/pldoc/package/http.html) saying that:
>
> "The goal is called with the parsed request (see section 3.11) as
> argument and current_output set to a temporary buffer."
>
> However I couldn't find any way to flush this "temporary buffer" -
> assuming that it would help.
>
> Any suggestions?
>
>