2014-11-03 4 views
2

나는 Catalyst, AnyEvent, Websocket 앱에서 타임 아웃 시나리오를 작성하려고합니다. 그 동안 나는이 끝난 후 (더 이상 WS을 오는 프레임)의 비활성 몇 초를 가정 해 봅시다해야 AnyEvent-> 타이머가 AnyEvent :: Handle과 함께 작동하지 않습니까?

AnyEvent->timer 

을 사용하고 있습니다.

문제는 내 타이머가 실행되지 않습니다되어있다 :

my $w = AnyEvent->timer (after => 3, 
         cb => sub { 
    warn "TIMEOUT!"; 
}); 

$self->{server} = Protocol::WebSocket::Handshake::Server->new_from_psgi(
          $c->req->env) or die $c->log->fatal($!); 

$self->{handle} = AnyEvent::Handle->new(
    fh => $c->req->io_fh, 
    on_error => sub { 
     my ($hd, $fatal, $msg) = @_; 
     $clean_up->(); 
    } 
); 

die $c->log->fatal("WS Server error: '$_'") 
     if $self->{server}->error; 

$self->{server}->parse($self->{handle}->fh); 
$self->{handle}->push_write($self->{server}->to_string); 

$self->{handle}->on_read(sub { 
    (my $frame = $self->{server}->build_frame)->append($_[0]->rbuf); 

    while (my $frame_msg = $frame->next) { 
     ... 
    } 

타이머 콜백이 실행되지 않습니다. 내 생각 엔 타이머가 다른 이벤트 루프 (AnyEvent :: Handle) 내에서 작동하지 않는다고 생각하십니까?

+0

당신이 프로토콜 :: 웹 소켓 :: 핸드 셰이크 :: 서버를 사용하지 않는 문제에 대한 실행 가능한 데모를 만들 수 있을까요? (예를 들어,'perl -E '$ | = 1에서 파이프 될 수있는 STDIN을 읽을 수있다; abc; sleep 4; "def";') 문제는 Protocol WebSocket :: Handshake :: Server, 어떤 경우에는 언급해야 함). – ikegami

+0

질문에 게시 된 모든 코드가 함수에있는 경우 해당 함수가 종료 되 자마자 $ w가 범위를 벗어나 타이머를 취소합니다. – TheAmigo

답변

3

타이머 처리를위한 이벤트 루프에 실제로 들어가고 있습니까? 코드 스 니펫은이를 나타내지 않습니다. 또한

, AnyEvent::Handle는 비활성 시간 제한을 내장했습니다

 timeout => $fractional_seconds 
      If non-zero, then this enables an "inactivity" timeout: whenever 
      this many seconds pass without a successful read or write on the 
      underlying file handle, the "on_timeout" callback will be invoked 
      (and if that one is missing, a non-fatal "ETIMEDOUT" error will 
      be raised). 

      Note that timeout processing is also active when you currently do 
      not have any outstanding read or write requests: If you plan to 
      keep the connection idle then you should disable the timout 
      temporarily or ignore the timeout in the "on_timeout" callback, 
      in which case AnyEvent::Handle will simply restart the timeout. 

      Zero (the default) disables this timeout. 

     on_timeout => $cb->($handle) 
      Called whenever the inactivity timeout passes. If you return from 
      this callback, then the timeout will be reset as if some activity 
      had happened, so this condition is not fatal in any way. 
관련 문제