#!/opt/perl/bin/perl use AnyEvent; use AnyEvent::Handle; use AnyEvent::MP; my $resolved_cv = AnyEvent->condvar; my $client_port = create_port; my $server_node = "localhost:1299#"; snd $server_node, wkp => "chatter", "$client_port", "resolved"; my $server_port; # setup a receiver callback for the 'resolved' message: $client_port->rcv (resolved => sub { my ($client_port, $type, $chatter_port_id) = @_; print "Resolved the server port 'chatter' to $chatter_port_id\n"; $server_port = $chatter_port_id; $resolved_cv->send; 1 }); # lets block the client until we resolved the server port. $resolved_cv->recv; # now setup another receiver callback for the chat messages: $client_port->rcv (message => sub { my ($client_port, $type, $msg) = @_; print "chat> $msg\n"; 0 }); # send the server a 'join' message: snd $server_port, join => "$client_port"; sub send_message { my ($msg) = @_; snd $server_port, message => $msg; } # make an AnyEvent condition variable for the 'quit' condition # (when we want to exit the client). my $quit_cv = AnyEvent->condvar; my $stdin_hdl = AnyEvent::Handle->new ( fh => \*STDIN, on_read => sub { my ($hdl) = @_; $hdl->push_read (line => sub { my ($hdl, $line) = @_; if ($line =~ /^\/quit/) { # /quit will end the client $quit_cv->send; } else { send_message ($line); } }); } ); $quit_cv->recv;