Skip to content

Instantly share code, notes, and snippets.

@Altai-man
Created April 1, 2019 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Altai-man/33b94415598ffca6114f7f16c53aa93b to your computer and use it in GitHub Desktop.
Save Altai-man/33b94415598ffca6114f7f16c53aa93b to your computer and use it in GitHub Desktop.
use Cro::WebSocket::Client;
use Cro::HTTP::Router;
use Cro::HTTP::Router::WebSocket;
use Cro::HTTP::Server;
my $application = route {
get -> 'chat' {
web-socket(-> $incoming {
supply {
whenever $incoming -> $message { # This will be executed even after server's `stop` call
note "Received" ~ await $message.body-text;
}
}
})
}
}
my Cro::Service $hello = Cro::HTTP::Server.new:
:host<localhost>, :port<10000>, :$application;
$hello.start;
note "Started the server";
start react whenever Promise.in(5) {
note "Closing the server";
$hello.stop;
note "Closed the server";
}
my $uri = 'ws://localhost:10000/chat';
my $conn = await Cro::WebSocket::Client.connect($uri);
react {
whenever Supply.interval(1) {
note $_;
note "Sending $_";
$conn.send($_) # Client has exception here, but it won't blow up, has to be $_.Str
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment