Jump to content

Chat server: Difference between revisions

add Perl 6
m (→‎{{header|Go}}: fix mistake in previous change)
(add Perl 6)
Line 1,477:
}
</lang>
 
=={{header|Perl 6}}==
 
{{improve|Perl 6|
* I'm not sure if the writes to <tt>%connections</tt> and <tt>$name</tt> can cause race conditions, as I don't understand the <tt>react</tt>/<tt>whenever</tt> idiom well enough yet.
* When a client sends bytes that are not valid UTF8, the server dies with "<code>Unhandled exception: Malformed UTF-8</code>" and I can't seem to find a way to prevent it.
}}
 
{{trans|Python}}
{{works with|Rakudo|2016.07}}
<lang perl6>#!/usr/bin/env perl6
 
react {
my %connections;
whenever IO::Socket::Async.listen('localhost', 4004) -> $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;
}
}
}
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;
}
}
}</lang>
 
=={{header|PicoLisp}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.