Skip to content

Commit 7679a0c

Browse files
Jarkko Haapalainenzoffixznet
authored andcommitted
Fixed server and client examples for IO::Socket::Async (#1628)
* IO::Socket::Async server example using heredoc and maybe more clearer for newcomers * IO::Socket::Async client example tweaks Couple of tweaks: - There are issues with libuv when it defaults ipv6 when using localhost name. We can workaround that just using 127.0.0.1 instead of localhost name. - Added newline for print
1 parent 5ed8953 commit 7679a0c

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

doc/Type/IO/Socket/Async.pod6

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,40 @@ http server that listens on port 3333:
1515
=begin code :skip-test<IO>
1616
react {
1717
whenever IO::Socket::Async.listen('0.0.0.0', 3333) -> $conn {
18-
whenever $conn.Supply.lines {
19-
say .Str;
20-
21-
{ try $conn.print(.Str ~ "\n") } for
22-
"HTTP/1.1 200 OK",
23-
"Content-Type: text/html; charset=UTF-8",
24-
"Content-Encoding: UTF-8",
25-
"",
26-
"<html><body>Hello World!</body></html>";
27-
18+
whenever $conn.Supply.lines -> $line {
19+
$conn.print: qq:heredoc/END/;
20+
HTTP/1.1 200 OK
21+
Content-Type: text/html; charset=UTF-8
22+
Content-Encoding: UTF-8
23+
24+
<html>
25+
<body>
26+
<h1>Hello World!</h1>
27+
<p>{ $line }</p>
28+
</body>
29+
</html>
30+
END
2831
$conn.close;
2932
}
30-
CLOSE {
31-
note „{now} connection closed“;
33+
}
34+
CATCH {
35+
default {
36+
say .^name, ': ', .Str;
37+
say "handled in $?LINE";
3238
}
3339
}
34-
CATCH { default { say .^name, ': ', .Str; say "handled in $?LINE"; } }
3540
}
3641
=end code
3742
3843
And a client that connects to it, and prints out what the server answers:
3944
4045
=begin code
41-
await IO::Socket::Async.connect('localhost', 3333).then( -> $promise {
46+
await IO::Socket::Async.connect('127.0.0.1', 3333).then( -> $promise {
4247
given $promise.result {
43-
.print('Hello, Perl 6');
48+
.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n");
4449
react {
4550
whenever .Supply() -> $v {
46-
$v.say;
51+
$v.print;
4752
done;
4853
}
4954
}

0 commit comments

Comments
 (0)