Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by xtpu2 (Acolyte)
on Apr 09, 2014 at 14:39 UTC ( #1081655=note: print w/ replies, xml ) Need Help??


in reply to Re^3: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
in thread PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help

Wow, I think you've nailed it! After reading your post, I started digging into that and came across this, discussing this exact issue:

https://github.com/miyagawa/Twiggy/issues/33

I'm posting the modified code here so as not to make the OP confusing. The new code no longer closes the connection immediately, but the timer, for some reason only fires ONCE (the first time), whereas I would like it to keep going. I'm guessing this has something to do with the line undef $w; but when I get rid of it, the timer doesn't fire at all.

#!/usr/bin/perl use strict; use warnings; use Plack::Builder; use SockJS; use AnyEvent; builder { mount '/echo' => SockJS->new( handler => sub { my ($session) = @_; $session->on( 'data' => sub { my $session = shift; $session->write('got your message'); } ); $session->write('connected'); my $w_cond = AnyEvent->condvar; $w_cond->cb( sub { warn $_[0]->recv } ); my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { undef $w; # cleanup $session->write('5 seconds have passed'); $w_cond->send(); } ); }; ); };


Comment on Re^4: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
Select or Download Code
Re^5: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
by Corion (Pope) on Apr 09, 2014 at 15:41 UTC

    You need to mention the timer variable $w inside the subroutine to keep it alive.

    Either move it into a global variable or just mention it in the subroutine so it closes over the value. Even better would be to clean up the timer based on some condition.

      So this is the final version of the timer code that works:

      ... my $w_cond = AnyEvent->condvar; $w_cond->cb( sub { warn $_[0]->recv } ); my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { undef $w if (0); $session->write('5 seconds have passed'); $w_cond->send(); } ); ...

      I changed undef $w; to undef $w if (0);. Now the big question is why does this work, but simply commenting out undef $w; causes the timer to not work? Is there a logical reason here, or is it voodoo? By "voodoo" I mean, code that has to be typed a certain way "just because".

        Now is the moment you want to read about "closures" in Perl. That is why I told you about "mentioning" $w in the subroutine or making $w a global variable.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-07-25 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls