in reply to WebSocket idle

Here are some changes I needed, I think for SSL renegotiation. I wasn't able to find much about non-blocking sockets with SSL, so hopefully someone can confirm if I did this right.

#!/usr/bin/perl use strict; use warnings; use IO::Socket::SSL; use IO::Framed; use IO::Select; 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; use Time::HiRes qw(time usleep); 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"; 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 +); my $subscribe = { type => 'subscribe', product_ids => \@prods, channels => ['full'] }; my $payload = encode_json($subscribe); print $payload."\n"; syswrite( $inet, Net::WebSocket::Frame::text->new( payload => $payload, mask => Net::WebSocket::Mask::create(), )->to_bytes() ); #See below about IO::Framed my $iof = IO::Framed->new($inet); $iof->allow_empty_read(); my $parser = Net::WebSocket::Parser->new( $iof ); my $ept = Net::WebSocket::Endpoint::Client->new( parser => $parser, out => $iof, ); $inet->blocking(0); my $sel = IO::Select->new($inet); my $lastseq; my $seq; while(1) { $sel->can_read(); my $frame = $ept->get_next_message(); if($frame) { # we are catching up $frame = $frame->get_payload(); load_frame($frame); } elsif($seq == $lastseq) { # no new data since last iteration if($inet->connected()) { next if $SSL_ERROR == SSL_WANT_READ; if ( $SSL_ERROR == SSL_WANT_WRITE ) { # SSL renegotiation $sel->can_write; next; } # wait for more data usleep(100000); } else { die("WebSocket connection lost!"); } } else { # 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); $lastseq = $seq; $seq = $$data{'sequence'}; print Dumper($data); }

Replies are listed 'Best First'.
Re^2: WebSocket idle
by noxxi (Pilgrim) on Jan 20, 2018 at 11:02 UTC
    > I wasn't able to find much about non-blocking sockets with SSL, so hopefully someone can confirm if I did this right.

    For dealing with non-blocking sockets please see the relevant section in the documentation. Please note especially that you cannot simply rely on IO::Select as you did to get notified if new data are available, because data might still be buffered internally in the SSL stack. See the section about common usage errors for details.

      That sample code is where my code changes came from. I'm relying on the websocket module to notify me when data is available, right? I can't read 1 byte at a time like they did because the websocket messages are read with $ept->get_next_message(); Before I made the change, it would get caught in a loop without reading any new data, I'm guessing because SSL needed to be renegotiated like I read in the doc I did find, preventing the websocket module from seeing more data. My change with the select seems to have resolved it.

      I also added a 100ms sleep if there is no message received since the last iteration based on sequence numbers in the messages, but that may never happen because I don't think it goes more than a second without a message.

        > That sample code is where my code changes came from

        Please look closer at the example code in IO::Socket::SSL you've based your code from. The example code is specifically dealing with the case that there are still buffered data (using pending) before calling can_read again. Your code misses this part. While the code then will work for cases where each websocket frame is in its own SSL frame it will spuriously fail in case multiple websocket frame are contained in one SSL frame.

        > Before I made the change, it would get caught in a loop without reading any new data, I'm guessing because SSL needed to be renegotiated like I read in the doc I did find...

        In your first code you made the socket non-blocking but then did not check for the socket to be readable at all (can_read) and instead run a busy loop where it always tried to read from the socket and most times immediately failed with EAGAIN since no data where available on the socket. Adding the can_read fixed this but introduced the problem that you don't deal properly with multiple websocket frames inside a single SSL frame. The part about renegotiation instead (i.e. dealing with SSL_WANT_WRITE) is probably unused in most cases since usually no renegotiation will happen on the socket.