Сначала простенький пример:
----------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use threads; use threads::shared;
use AnyEvent;
use Net::XMPP2::IM::Connection;
my $dest='myjabberaccount@gmail.com';
my $j = AnyEvent->condvar;
#my $timer;
my $thr2;
my $Con = Net::XMPP2::IM::Connection->new ( jid => 'testaccount@gmail.com', password => 'someC00lPasswdd',
initial_presence => -10, debug => 1);
sub ListenForMessages {
async {
while (1){
print "Sending message inside timer\n";
my $immsg = Net::XMPP2::IM::Message->new (to => $dest, body => "ttteessstt from timer");
$immsg->send ($Con);
sleep 5;
}
};
# $timer = AnyEvent->timer (after => 5, cb => sub {
# print "Sending message inside timer\n";
# my $immsg = Net::XMPP2::IM::Message->new (to => $dest, body => "ttteessstt from timer");
# $immsg->send ($Con);
# sleep 5;
# ListenForMessages();
# });
}
$Con->reg_cb (
session_ready => sub {
my ($con) = @_;
print "Connected as " . $con->jid . "\n";
print "Sending message to $dest:\n$msg\n";
my $immsg = Net::XMPP2::IM::Message->new (to => $dest, body => "ttteessstt"); $immsg->send ($Con);
ListenForMessages();
},
message => sub {
my ($con, $msg) = @_;
print "Message from " . $msg->from . ":\n" . $msg->any_body . "\n---\n";
},
error => sub {
my ($con, $error) = @_;
warn "Error: " . $error->string . "\n";
},
disconnect => sub {
my ($con, $h, $p, $reason) = @_;
warn "Disconnected from $h:$p: $reason\n";
$j->broadcast;
}
);
print "Trying to connect...\n";
$Con->connect ();
$j->wait;
--------------------------------------------------------------
Теперь пояснение: Приходит только "ttteessstt". Ни одна из мессаг "ttteessstt from timer" даже не пытается отправиться (судя по tcpdump). И ни через таймер, ни через async{} не работает. :(
Кто что может подсказать?
Так никто и не ответил, пишу потомкам в назидание:
--------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use utf8;
use AnyEvent;
use Net::XMPP2::Client;
use Net::XMPP2::IM::Message;
my $dest='...';
my $j = AnyEvent->condvar;
my $con;
my $cl = Net::XMPP2::Client->new ();
$cl->add_account ('...', '...');
$cl->reg_cb (
connected => sub {
my ($cl, $acc) = @_;
print "Connected as " . $acc->jid . "\n";
my $c = $acc->connection();
$c->set_default_iq_timeout (30);
$con = $c;
$j->broadcast;
0;
},
message => sub {
my ($con, $acc, $msg) = @_;
print "Message from " . $msg->from . ":\n" . $msg->any_body . "\n---\n";
},
error => sub {
my ($cl, $acc, $err) = @_;
warn "Error: " . $err->string . "\n";
},
disconnect => sub {
my ($con, $h, $p, $reason) = @_;
warn "Disconnected from $h:$p: $reason\n";
$j->broadcast;
}
);
print "Trying to connect...\n";
$cl->start;
$j->wait;
my $J = AnyEvent->condvar;
my $timer;
sub ListenForMessages{
$timer = AnyEvent->timer (after => 3, cb => sub {
print "Sending message inside timer\n";
my $immsg = Net::XMPP2::IM::Message->new (to => $dest, body => "Sending message from timer ");
$immsg->send($con);
ListenForMessages();
});
}
ListenForMessages();
$J->wait;
--------------------------------------------------------------
Ключевой момент - нужно было создать ещё один AnyEvent->condvar.