I've been using event loops, but I haven't been able to get the loop to triage to my sub when there are no other events. I've only been able to force my sub to run as a periodic event. Here is the code I have now, but it doesn't actually read any frames. Maybe the subscription isn't being sent correctly? It shows the logic I am trying to accomplish, though.

#!/usr/bin/perl use strict; use IO::Socket::SSL; use IO::Framed; use Net::WebSocket::Frame::text; use Net::WebSocket::Parser; use Net::WebSocket::Endpoint::Client; use Net::WebSocket::Handshake::Client (); use Net::WebSocket::HTTP_R (); use HTTP::Response; use JSON::XS; use Data::Dumper; my @prods = qw(BTC-USD LTC-USD ETH-USD); my $host = 'ws-feed.gdax.com'; my $wssurl = "wss://$host/"; my $port = 443; my $inet = IO::Socket::SSL->new("$host:$port") or die "failed connect or ssl handshake: $!\n$SSL_ERROR"; $inet->blocking(0); my $handshake = Net::WebSocket::Handshake::Client->new( uri => $wssurl ); print "REQ HEADERS:\n".$handshake->to_string()."\n"; syswrite $inet, $handshake->to_string() or die $!; my $buffer = ''; my $cnt = 0; my $line = <$inet>; while($line ne "\r\n") { $buffer.= $line; $line = <$inet>; } print "RESP HEADERS:\n$buffer\n"; my $resp = HTTP::Response->parse($buffer); Net::WebSocket::HTTP_R::handshake_consume_response( $handshake, $resp +); #See below about IO::Framed my $iof_r = IO::Framed->new($inet); $iof_r->allow_empty_read(); my $parser = Net::WebSocket::Parser->new( $iof_r ); my $iof_w = IO::Framed->new($inet); my $ept = Net::WebSocket::Endpoint::Client->new( parser => $parser, out => $iof_w, ); my $subscribe = { type => 'subscribe', product_ids => \@prods, channels => ['full'] }; my $payload = encode_json($subscribe); print $payload."\n"; $iof_w->write( Net::WebSocket::Frame::text->new( payload => $payload ) ); while(1) { while(1) { # we are catching up print "get next frame\n"; my $frame = $ept->get_next_message(); print "\$frame = \"$frame\"\n"; last unless($frame); load_frame(decode_json($frame)); } # we are caught up somethinghard(); # catch up again } sub somethinghard { print "something hard\n"; sleep 1; } sub load_frame { my ($frame) = @_; my $data = decode_json($frame); print Dumper($data); }

In reply to Re^2: WebSocket idle by Anonymous Monk
in thread WebSocket idle by chris212

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.