Created
July 22, 2009 07:50
-
-
Save sekimura/151869 to your computer and use it in GitHub Desktop.
simple chat server by using AnyEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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