Created
April 1, 2019 22:19
-
-
Save Altai-man/33b94415598ffca6114f7f16c53aa93b 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
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