POE:自定义 POE::Component 中的定期服务器警报

POE: recurring server Alarms in a custom POE::Component

我正在尝试使用 perl POE 设置客户端-服务器应用程序。

阅读 CPAN and trying some of the examples of the cookbook, finaly reccuring Alarms 上的一些 POE 文档后,我遇到以下问题:

我的自定义组件中的事件在(客户端)会话中触发。

package POE::Component::OXO_TCP_Server;

use strict;
use warnings;
use POE;
use POE::Filter::Reference;
use POE::Component::Server::TCP;

sub new {
    my ($proto, %args) = @_;
    my $class = ref ($proto) || $proto;

    my $send_up_intervall   = 10;
    my %default = (
      Alias         => "oxo_tcp_server",
      Address       => "localhost",
      Port          => 12345,
      ClientFilter  => "POE::Filter::Reference",
      ClientInput   => \&handle_client_input,

      ClientDisconnected => sub {
          $_[KERNEL]->yield("shutdown");
      },

      SessionParams => [ options => { debug => 1, trace => 1 } ],

      InlineStates => {
        _start => sub {
          $_[HEAP]->{next_alarm_time} = int(time()) + 1;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        },

        tick => sub {
          print "tick at ", time(), "\n";
          $_[HEAP]->{next_alarm_time}++;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        }, 
      }
    );

    # add \%args; to \%default
    my $self = POE::Component::Server::TCP->new(%default);
    return $self;
}

sub handle_client_input {...}

1;

当我向服务器发送一些输入时事件

=== 8071 === 2 -> tick (from ../lib/POE/Component/OXO_TCP_Server.pm at 65) tick at 1433857644
...

被解雇了。

但我希望他们独立于客户被解雇(仅按时间)。

我该怎么做?我没有在文档或食谱中找到它。

为了避免过载 _start 我还尝试在

中初始化计时器
Started => sub {
  $_[HEAP]->{next_alarm_time} = int(time()) + 1;
  $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
},  

在第一个视图中看起来它正在工作,因为调试显示:

=== 8093 === 1 -> tick (from ../lib/POE/Component/OXO_TCP_Server.pm at 47)

但事实并非如此。可能它仍然是错误的会话。想法?

谢谢。

我找到了一种方法。我添加了一个额外的会话:

POE::Session->create(
  inline_states => {
        _start => sub {

          $_[HEAP]->{next_alarm_time} = int(time()) + 1;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        },

        tick => sub {
          print "tick at ", time(), "\n";
          $_[HEAP]->{next_alarm_time}++;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        },
  },
);

这种方式独立于 POE::Component::Server::TCP。可能有 other/better 种方法。