Skip to content

Instantly share code, notes, and snippets.

@sekimura
Created July 22, 2009 07:50
Show Gist options
  • Save sekimura/151869 to your computer and use it in GitHub Desktop.
Save sekimura/151869 to your computer and use it in GitHub Desktop.
simple chat server by using AnyEvent
#!/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) = @_;
syswrite $fh, "; you have " . scalar(keys %conns) . " buddies\015\012";
my $hdl = AnyEvent::Handle->new(
fh => $fh,
);
my $id = "$host:$port";
$conns{$id} = $fh;
my $reader; $reader = sub {
my $line = $_[1];
for my $xid (grep {$_ ne $id} keys %conns) {
syswrite $conns{$xid}, "$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