ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/eg/chat_server
Revision: 1.3
Committed: Fri Aug 7 09:07:37 2009 UTC (14 years, 9 months ago) by elmex
Branch: MAIN
CVS Tags: rel-0_6
Changes since 1.2: +9 -1 lines
Log Message:
changed chat client/server example to the new initialise api and added monitor.

File Contents

# User Rev Content
1 root 1.2 #!/opt/bin/perl
2    
3 elmex 1.1 use AnyEvent;
4     use AnyEvent::MP;
5    
6 elmex 1.3 initialise_node "127.0.0.1:1299";
7    
8     print "initialized server\n";
9 root 1.2
10     my $chatter_port = port;
11 elmex 1.1
12 root 1.2 reg $chatter_port, "chatter";
13 elmex 1.1
14     my %client_ports;
15    
16 root 1.2 rcv $chatter_port,
17     join => sub {
18     my ($tag, $client_port) = @_;
19 elmex 1.1
20 root 1.2 print "got new client port: $client_port\n";
21     $client_ports{$client_port} = 1;
22 elmex 1.1
23 elmex 1.3 mon $client_port, sub {
24     my (@reason) = @_;
25     print "client disconnected: " . join (', ', @reason) . "\n";
26     delete $client_ports{$client_port}
27     };
28    
29 root 1.2 0
30     },
31     message => sub {
32     my ($tag, $msg) = @_;
33 elmex 1.1
34 root 1.2 print "message> $msg\n";
35 elmex 1.1
36 root 1.2 snd $_, message => $msg
37     for keys %client_ports;
38 elmex 1.1
39 root 1.2 0
40     };
41 elmex 1.1
42     AnyEvent->condvar->recv;