Forked from sekimura/simple chat server by using AnyEvent
Created
April 30, 2014 12:24
-
-
Save keedi/dd40c9617e18220e3381 to your computer and use it in GitHub Desktop.
This file contains hidden or 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