Last active
April 26, 2018 14:39
-
-
Save ktown007/2af8aa40fa9b0867d6065fc6fbc7337a to your computer and use it in GitHub Desktop.
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/bun/env perl6 | |
my $host = '127.0.0.1' ; | |
my %connections ; | |
for 1 .. 1000 { | |
my $message = "{time}-$_" ; | |
%connections{$message} = conn($message) ; | |
sleep 0.01 ; | |
} | |
sleep 1 ; | |
for %connections.kv -> $name, $conn { | |
#say "$name bye" ; | |
await $conn.print: "bye\n" ; | |
sleep 0.01 ; | |
$conn.close; | |
} | |
say "all closed" ; | |
sleep 1 ; | |
sub conn($msg) { | |
my $conn = await IO::Socket::Async.connect( $host , 3333); | |
await $conn.print: "$msg\n"; | |
$conn.Supply.tap(-> $v { | |
print "$msg $v" ; | |
}); | |
return $conn ; | |
} | |
CATCH { | |
default { | |
say .^name, ': ', .Str; | |
say "handled in $?LINE"; | |
} | |
} |
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/env perl6 | |
react { | |
my %connections; | |
whenever IO::Socket::Async.listen('localhost', 3333) -> $conn { | |
my $name; | |
#$conn.print: "Please enter your name: "; | |
whenever $conn.Supply.lines -> $message { | |
if !$name { | |
if %connections{$message} { | |
#$conn.print: "Name already taken, choose another one: "; | |
} | |
else { | |
$name = $message; | |
%connections{$name} = $conn; | |
broadcast "+++ %s arrived +++", $name; | |
} | |
} | |
else { | |
broadcast "%s> %s", $name, $message; | |
} | |
LAST { | |
broadcast "--- %s left ---", $name; | |
%connections{$name}:delete; | |
$conn.close ; | |
} | |
QUIT { | |
default { | |
say "oh no, $_"; | |
} | |
} | |
} | |
#Supply.interval( 2 ).tap: { # push keepalive messages | |
# if $name { $conn.print: "$name : alive {time}\n" ;} | |
#}; | |
} | |
sub broadcast ($format, $from, *@message) { | |
my $text = sprintf $format, $from, |@message; | |
#say $text; | |
for %connections.kv -> $name, $conn { | |
$conn.print: "$text\n" if $name ne $from; | |
} | |
} | |
} | |
CATCH { | |
default { | |
say .^name, ': ', .Str; | |
say "handled in $?LINE"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unhandled exception in code scheduled on thread 8
Unhandled exception in code scheduled on thread 9
No such method 'throw' for invocant of type 'Str'
in method throw at SETTING::src/core/Exception.pm line 57
in block at SETTING::src/core/Supply.pm line 90
in method run-catch at SETTING::src/core/Supply.pm line 1890
in any at /opt/rakudo-star-2018.01/share/perl6/runtime/CORE.setting.moarvm line 1
in method throw at SETTING::src/core/Exception.pm line 54
in block at SETTING::src/core/Exception.pm line 2728
in block at SETTING::src/core/Supply.pm line 90
in block at SETTING::src/core/Supply.pm line 1938
in block at SETTING::src/core/Supply.pm line 1974
in method run-with-updated-recursion-list at SETTING::src/core/Lock/Async.pm line 170
in method protect-or-queue-on-recursion at SETTING::src/core/Lock/Async.pm line 136
in method run-supply-code at SETTING::src/core/Supply.pm line 1972
in block at SETTING::src/core/Supply.pm line 1932
in block at SETTING::src/core/IO/Socket/Async.pm line 83
in method protect at SETTING::src/core/Lock/Async.pm line 121
in block at SETTING::src/core/IO/Socket/Async.pm line 80
in block at SETTING::src/core/ThreadPoolScheduler.pm line 233
in block at SETTING::src/core/ThreadPoolScheduler.pm line 232
in method run-one at SETTING::src/core/ThreadPoolScheduler.pm line 230
in block at SETTING::src/core/ThreadPoolScheduler.pm line 293