Domanda

Sto usando POE per costruire un sistema che colma diversi protocolli (HTTP, IRC, XMPP) e vorrei usare Poe per guidare un singolo ciclo di eventi che gestisce questi protocolli. Posso farlo in modo sicuro e, in tal caso, come?

È stato utile?

Soluzione

Si, puoi. Leggi Questo articolo, dovrebbe aiutarti molto. Inoltre, ecco il codice Esempio di IRC e HTTP in esecuzione: Ricorda, è necessario configurare tutto prima di eseguire 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();

E tu puoi eventi di trasmissione tra i tuoi compiti.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top