如何从raku中的IO :: socket :: async获得一行?

发布于 2025-02-06 04:08:43 字数 229 浏览 0 评论 0原文

因此,我有:

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

如何仅接收服务器的第一行?

我可以在时使用并在其中使用一些逻辑,但是有一种简单的方法,对吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2025-02-13 04:08:44

如果您真的只想要第一行,并且不在乎其余的响应,那么您可以做到这一点:

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

也就是说,获取代表响应流的supply,是否将其拆分为行(这导致了行的供应),并采用到达的第一行。一行以外收到的任何数据都将被丢弃。

io :: socket :: async在提供数据数据包时起作用。它没有提供面向线路的接口;如果协议确实如此简单,就像逐条阅读,那么每当$ conn.supply.lines -lines-> $ line {} 将执行此操作(并且处理行为正确分配的行)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文