File: stdio-client.pl

package info (click to toggle)
libnet-async-websocket-perl 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156 kB
  • sloc: perl: 519; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/perl

use v5.14;
use warnings;

use IO::Async::Loop;
use IO::Async::Stream;
use Net::Async::WebSocket::Client;

my $HOST = shift @ARGV or die "Need HOST";
my $PORT = shift @ARGV or die "Need PORT";

my ( $client, $stdio );

$client = Net::Async::WebSocket::Client->new(
   on_text_frame => sub {
      my ( $self, $frame ) = @_;
      $stdio->write( $frame );
   },
);

$stdio = IO::Async::Stream->new_for_stdio(
   on_read => sub {
      my ( $self, $buffref ) = @_;
      $client->send_text_frame( $$buffref );
      $$buffref = "";
   },
);

my $loop = IO::Async::Loop->new;
$loop->add( $client );
$loop->add( $stdio );

$client->connect(
   host => $HOST,
   service => $PORT,
   url => "ws://$HOST:$PORT/",
)->get;

print "Connected; go ahead...\n";

$loop->run;