1 |
root |
1.2 |
#!/opt/bin/perl |
2 |
|
|
|
3 |
root |
1.7 |
# Usage: ./chat_server |
4 |
|
|
# implement a chat server using "traditional message passing" |
5 |
|
|
|
6 |
root |
1.4 |
use common::sense; |
7 |
elmex |
1.1 |
use AnyEvent::MP; |
8 |
root |
1.6 |
use AnyEvent::MP::Global; |
9 |
elmex |
1.1 |
|
10 |
root |
1.8 |
configure; |
11 |
elmex |
1.3 |
|
12 |
root |
1.4 |
my %clients; |
13 |
root |
1.2 |
|
14 |
root |
1.4 |
sub msg { |
15 |
|
|
print "relaying: $_[0]\n"; |
16 |
|
|
snd $_, $_[0] |
17 |
|
|
for values %clients; |
18 |
|
|
} |
19 |
|
|
|
20 |
root |
1.7 |
our $server = port; |
21 |
|
|
|
22 |
|
|
rcv $server, join => sub { |
23 |
|
|
my ($client, $nick) = @_; |
24 |
|
|
|
25 |
|
|
$clients{$client} = $client; |
26 |
root |
1.4 |
|
27 |
|
|
mon $client, sub { |
28 |
|
|
delete $clients{$client}; |
29 |
root |
1.7 |
msg "$nick (quits, @_)"; |
30 |
root |
1.4 |
}; |
31 |
root |
1.7 |
msg "$nick (joins)"; |
32 |
|
|
}; |
33 |
elmex |
1.1 |
|
34 |
root |
1.7 |
rcv $server, privmsg => sub { |
35 |
|
|
my ($nick, $msg) = @_; |
36 |
|
|
msg "$nick: $msg"; |
37 |
|
|
}; |
38 |
elmex |
1.3 |
|
39 |
root |
1.7 |
AnyEvent::MP::Global::register $server, "eg_chat_server"; |
40 |
elmex |
1.1 |
|
41 |
root |
1.4 |
warn "server ready.\n"; |
42 |
elmex |
1.1 |
|
43 |
root |
1.4 |
AE::cv->recv; |