Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyagawa/151871 to your computer and use it in GitHub Desktop.
Save miyagawa/151871 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Socket qw/tcp_server/;
use AnyEvent::Handle;
my %conns;
my $gaurd = tcp_server undef, 8191, sub {
my ($fh, $host, $port) = @_;
my $hdl = AnyEvent::Handle->new(
fh => $fh,
);
$hdl->push_write("; you have " . scalar(keys %conns) . " buddies\015\012");
my $id = "$host:$port";
$conns{$id} = $hdl;
my $reader; $reader = sub {
my $line = $_[1];
for my $xid (grep {$_ ne $id} keys %conns) {
$conns{$xid}->push_write("$id $line\015\012");
}
$hdl->push_read( line => $reader );
};
$hdl->push_read( line => $reader );
};
AnyEvent->condvar->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment