我正在使用POE构建一个桥接多个协议(HTTP,IRC,XMPP)的系统,并且我想使用POE来驱动处理这些协议的单个事件循环。我可以安全地这样做,如果是这样,怎么做?
答案 0 :(得分:4)
是的,你可以。阅读this article,它应该对您有所帮助。
这里还有一起运行的IRC和HTTP的代码示例:
请记住,在运行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();
您可以在任务之间broadcast events。