| 1 | 1 | ▶ | use v5.40; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use experimental 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Future; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Future::IO; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use PAGI::Nano; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # Ports PAGI's 18-bidirectional-websocket (and PAGI-Tools' websocket-bidirectional) | ||||||||||
| 9 | # to PAGI::Nano. $receive and $send are independent, so a single connection runs | ||||||||||
| 10 | # two concurrent branches: one echoes client messages, one pushes server ticks | ||||||||||
| 11 | # unsolicited. Future->wait_any ties them together so a disconnect cancels both. | ||||||||||
| 12 | # | ||||||||||
| 13 | # pagi-server app.pl | ||||||||||
| 14 | # websocat ws://127.0.0.1:5000/ | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | my $app = app { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | websocket '/' => async sub ($c) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | my $ws = $c->websocket; | |||||||||
| 19 | 1 | await $ws->accept; | |||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | my $incoming = async sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | await $ws->each_text(async sub ($text) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | await $ws->send_text("echo: $text"); | |||||||||
| 24 | 1 | }); | |||||||||
| 25 | 1 | }; | |||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | my $outgoing = async sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 28 | 1 | my $n = 0; | |||||||||
| 29 | 1 | ▶ | while ($ws->is_connected) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 1 | await $ws->send_text_if_connected('tick ' . ++$n); | |||||||||
| 31 | 1 | await Future::IO->sleep(1); | |||||||||
| 32 | } | ||||||||||
| 33 | 1 | }; | |||||||||
| 34 | |||||||||||
| 35 | 1 | await Future->wait_any($incoming->(), $outgoing->()); | |||||||||
| 36 | 1 | }; | |||||||||
| 37 | 1 | }; | |||||||||
| 38 | |||||||||||
| 39 | 1 | $app; | |||||||||