Perl and Html5 ServerSentEvents -


I am trying to implement an event sent by the server to change AJAX long voting in my application. In Perl script, the sleeping stream is blocking.

  #! / Opt / lampp / bin / perl print "content-type: text / event-stream \ n \ n"; While (1) {print "Event: Server-Time \ n"; My $ time = local time (); Print "Data: $ time \ n \ n"; } This code works but, with sleep it is not working. Just Streaming  

Loading Content Instead of Printing

HTML Snippet:

  & lt; Head & gt; & Lt; Script type = "text / javascript" & gt; Function invokeSSE () {var source = new eventsource ('test.pl'); Source.addEventListener ('server-time', function (e) {document.getElementById ('ticker'). InnerHTML = e.data + '' 
';}, false); Source.addEventListener ('Open', function (e) {warning ('open');}, false); Source.addEventListener ('Error', function (e) {if (e.readyState == EventSource. CLOSED) {Warning ("connection closed");}}, false); } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body Onload = "Invoice SSE ()" & gt; & Lt; Div id = "ticker" name = "ticker" & gt; [TIME] & lt; / Div & gt; & Lt; / Body & gt;

Sleep is always going to be blocked, it is by design.

You should make an event based application for this, for that you can use dancers, mozolich.

From:

  Use Mojo :: UserAgent; # A General Transaction My $ ua = Mojo :: UserAgent- & gt; new; My $ tx = $ ua- & gt; Build_TX (GET = & gt; 'http://example.com'); # Body to my $ body = 'Hello world!' Prepare; $ Tx-> Reiki- & gt; Header-> Material_ lamps (length $ body); # Start writing my $ drain directly from the drain callback; $ Drain = sub {my $ content = shift; My $ share = substr $ body, 0, 1, ''; $ Drain = undefined until the body $ length; $ Content- & gt; Write ($ Chuck, $ groove); }; $ Tx-> Req-> Content-> $ Groove; # Process Transactions $ tx = $ ua- & gt; Start ($ tx);    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -