Chat server: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 352:
}</lang>
 
=={{header|C#|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;
Line 2,221:
}
}</lang>
 
=={{header|Perl 6}}==
 
<div style="display:inline-block">{{trans|Python}}</div> (or at least started out that way)
{{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;
$conn.close ;
}
QUIT {
default {
say "oh no, $_";
}
}
}
}
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>
 
Notes:
* It operates asynchronously (using <tt>IO::Socket::Async</tt>), so a slow connection to one client won't affect other clients.
* It accepts messages encoded in UTF-8.
* It tokenizes the message streams at newline boundaries (using the <tt>Supply.lines</tt> method), which I think makes the most sense for a chat application.
 
=={{header|PicoLisp}}==
Line 2,531 ⟶ 2,476:
((client (current-input-port) (current-output-port)))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<div style="display:inline-block">{{trans|Python}}</div> (or at least started out that way)
{{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;
$conn.close ;
}
QUIT {
default {
say "oh no, $_";
}
}
}
}
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>
 
Notes:
* It operates asynchronously (using <tt>IO::Socket::Async</tt>), so a slow connection to one client won't affect other clients.
* It accepts messages encoded in UTF-8.
* It tokenizes the message streams at newline boundaries (using the <tt>Supply.lines</tt> method), which I think makes the most sense for a chat application.
 
=={{header|Ruby}}==
10,327

edits