是否允许同时存在多个 Perl POE 提供程序?

发布于 2024-12-03 21:29:27 字数 96 浏览 0 评论 0原文

我正在使用 POE 构建一个桥接多个协议(HTTP、IRC、XMPP)的系统,并且我想使用 POE 来驱动处理这些协议的单个事件循环。我可以安全地做到这一点吗?如果可以,怎么做?

I'm using POE to build a system that bridges several protocols (HTTP, IRC, XMPP), and I'd like to use POE to drive a single event loop that handles these protocols. Can I do this safely, and if so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拍不死你 2024-12-10 21:29:27

是的,你可以。阅读这篇文章,它应该对你有很大帮助。
这里还有 IRC 和 HTTP 一起运行的代码示例:
请记住,您需要在运行主循环之前设置所有内容: POE::Kernel->run()

#!/usr/bin/env perl
use warnings;
use strict;
use POE;

# Simple HTTP server

use POE::Component::Server::HTTP;

POE::Component::Server::HTTP->new(
  Port           => 32090,
  ContentHandler => {
    '/'      => \&http_handler
  }
);

sub http_handler {
    my ($request, $response) = @_;
    $response->code(RC_OK);
    $response->content("<html><body>Hello World</body></html>");
    return RC_OK;
}

# Dummy IRC bot on #bottest at irc.perl.org

use POE::Component::IRC;

my ($irc) = POE::Component::IRC->spawn();

POE::Session->create(
  inline_states => {
    _start     => \&bot_start,
    irc_001    => \&on_connect,
  },
);

sub bot_start {
  $irc->yield(register => "all");
  my $nick = 'poetest' . $ % 1000;
  $irc->yield(
    connect => {
      Nick     => $nick,
      Username => 'cookbot',
      Ircname  => 'POE::Component::IRC cookbook bot',
      Server   => 'irc.perl.org',
      Port     => '6667',
    }
  );
}

sub on_connect { $irc->yield(join => '#bottest'); }


# Run main loop

POE::Kernel->run();

并且您可以 在任务之间广播事件

Yes, you can. Read this article, it should help you much.
Also here's code example of IRC and HTTP running together:
Just remember, you need setup everything before you run mainloop: POE::Kernel->run()

#!/usr/bin/env perl
use warnings;
use strict;
use POE;

# Simple HTTP server

use POE::Component::Server::HTTP;

POE::Component::Server::HTTP->new(
  Port           => 32090,
  ContentHandler => {
    '/'      => \&http_handler
  }
);

sub http_handler {
    my ($request, $response) = @_;
    $response->code(RC_OK);
    $response->content("<html><body>Hello World</body></html>");
    return RC_OK;
}

# Dummy IRC bot on #bottest at irc.perl.org

use POE::Component::IRC;

my ($irc) = POE::Component::IRC->spawn();

POE::Session->create(
  inline_states => {
    _start     => \&bot_start,
    irc_001    => \&on_connect,
  },
);

sub bot_start {
  $irc->yield(register => "all");
  my $nick = 'poetest' . $ % 1000;
  $irc->yield(
    connect => {
      Nick     => $nick,
      Username => 'cookbot',
      Ircname  => 'POE::Component::IRC cookbook bot',
      Server   => 'irc.perl.org',
      Port     => '6667',
    }
  );
}

sub on_connect { $irc->yield(join => '#bottest'); }


# Run main loop

POE::Kernel->run();

And you can broadcast events between your tasks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文