Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help

by Corion (Pope)
on Apr 09, 2014 at 07:54 UTC ( #1081621=note: print w/ replies, xml ) Need Help??


in reply to PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help

In your code, you create the callback timer and immediately discard it, which prevents the timer from ever getting called:

sub { ... my $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { $session->write('5 seconds have passed'); $w_cond->send(); } }; }

You need to keep $w alive until the timer has fired. The easiest way is the following pattern:

... my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { undef $w; # cleanup! $session->write('5 seconds have passed'); $w_cond->send(); } }; ...

That way, the timer stays alive until it has fired.


Comment on Re: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
Select or Download Code
Re^2: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
by xtpu2 (Acolyte) on Apr 09, 2014 at 14:24 UTC
    Thanks for that advice also! I've modified the code in the OP, however, the behavior has stayed the same.

      Please don't modify your original code that much, as people then can't learn from seeing your code progress through the stages.

      The next step would be to add (much) more amounts of server-side logging. print or warn at every step of the process. At least add logging statements to the following points:

      mount '/echo' => SockJS->new( handler => sub { warn "Creating new session"; my ($session) = @_; $session->on( 'data' => sub { my $session = shift; warn "got new data"; $session->write('got your message'); warn "Wrote response"; } ); $session->write('connected'); warn "Wrote greeting"; my $w_cond = AnyEvent->condvar; my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { undef $w; # cleanup $session->write('5 seconds have passed'); warn "Wrote timer message"; $w_cond->send(); } ); $w_cond->recv; warn "Leaving"; }; );

        Sorry! I will stop doing that. So far, one of the issues has been resolved: the problem with connection closing immediately. The node with updated code is here.

        There is still a problem with the timer firing only once.

Re^2: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
by Anonymous Monk on Apr 09, 2014 at 14:56 UTC
    I believe you meant to say:
    my $w; sub { ... $w = AnyEvent->timer( ... }
    ie: you want $w outside of builder
      On another glance
      my $w; $w = AnyEvent->timer(
      is correct, but
      $w_cond->send();
      within your timer cb is not what you want. From AnyEvent:
      $w->recv; # enters "main loop" till $condvar gets ->send
      You want to ->recv when you are done with things. Adding another timeout timer for example would do the trick allowing your timer to run couple of times and eventually terminating your timer and the main loop after 30 seconds:
      my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { $session->write('5 seconds have passed'); } ); my $timeout_timer; $timeout_timer = AnyEvent->timer( after => 0, interval => 30, cb => sub { undef $w; undef $timeout_timer; $session->write('Timeout after 30 seconds'); $w_cond->send(); } );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081621]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-09-20 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls