Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive just one line from an IO::Socket::Async in Raku?

Tags:

raku

So, I have this:

    my $conn = await IO::Socket::Async.connect('127.0.0.1', 12340);  
    $conn.print: "GET /rest HTTP/1.1\r\n\r\n";

How to receive just the first line from the server ?

I could use whenever and put some logic in it, but there's a simpler way, right ?

like image 538
zentrunix Avatar asked Oct 18 '25 02:10

zentrunix


1 Answers

If you really only want the first line, and don't care about the rest of the response, then you can do this:

my $first-line = await $conn.Supply.lines.first;

That is, get the Supply representing the response stream, have it split into lines (which results in a Supply of lines), and take the first line that arrives. Any data received beyond the first line will be discarded.

IO::Socket::Async works in terms of providing packets of data as they arrive. It doesn't get into providing a line-oriented interface; if a protocol really is so simple as reading line by line, then a react whenever $conn.Supply.lines -> $line { } will do it (and handles lines split over packets correctly).

like image 67
Jonathan Worthington Avatar answered Oct 20 '25 20:10

Jonathan Worthington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!