Skip to content

Instantly share code, notes, and snippets.

@kanatohodets
Created October 8, 2015 20:42
Show Gist options
  • Save kanatohodets/04c3486cfa5db983d6dc to your computer and use it in GitHub Desktop.
Save kanatohodets/04c3486cfa5db983d6dc to your computer and use it in GitHub Desktop.
use v6;
my $message-bus = Supply.new();
sub handle-socket ($id, $sock) {
$message-bus.emit("* $id has connected\n");
react {
whenever $message-bus -> $message {
$sock.print($message);
}
whenever $sock.chars-supply -> $bytes {
$message-bus.emit("$id: $bytes");
LAST {
$message-bus.emit("* $id has disconnected\n");
return;
}
}
}
}
my $max-id = 0;
say "listening on port 3005 ... ";
react {
whenever IO::Socket::Async.listen('localhost', 3005) -> $sock {
my $id = $max-id++;
start {
handle-socket($id, $sock);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment