| 1 |
#!/opt/bin/perl |
| 2 |
|
| 3 |
use common::sense; |
| 4 |
use AnyEvent::MP; |
| 5 |
use AnyEvent::MP::Global; |
| 6 |
|
| 7 |
initialise_node; |
| 8 |
|
| 9 |
AnyEvent::MP::Global::register $NODE, "eg_chat_server"; |
| 10 |
|
| 11 |
my %clients; |
| 12 |
|
| 13 |
sub msg { |
| 14 |
print "relaying: $_[0]\n"; |
| 15 |
snd $_, $_[0] |
| 16 |
for values %clients; |
| 17 |
} |
| 18 |
|
| 19 |
sub client_connect { |
| 20 |
my ($client, $name) = @_; |
| 21 |
|
| 22 |
mon $client; |
| 23 |
mon $client, sub { |
| 24 |
delete $clients{$client}; |
| 25 |
msg "$name (quits, @_)"; |
| 26 |
}; |
| 27 |
|
| 28 |
$clients{$client} = $client; |
| 29 |
|
| 30 |
msg "$name (joins)"; |
| 31 |
|
| 32 |
rcv $SELF, sub { msg "$name: $_[0]" }; |
| 33 |
} |
| 34 |
|
| 35 |
warn "server ready.\n"; |
| 36 |
|
| 37 |
AE::cv->recv; |