Need help in Perl sample

sn1sn1
edited July 2016 in General

Hello.

Please help me with sample Perl code, that will authorize and then request a profit_table. This code first send profit_table reqest (and receiving "Authorization reqired") and then send Authorize request:

#!/usr/local/bin/perl

use strict;
use warnings;
use v5.10;
use Mojo::UserAgent;
use Data::Dumper;

my $ua = Mojo::UserAgent->new;
$ua->websocket('wss://ws.binaryws.com/websockets/v3?l=en&app_id=XXXX' => sub {  #my app num here
    my ($ua, $tx) = @_;
    say 'WebSocket handshake failed!' and return unless $tx->is_websocket;

    $tx->on(json => sub {
        my ($tx, $data) = @_;
        say "Answer is: " . Dumper(\$data);
    });

    $tx->send({json => {
        authorize => 'xxxxxxxxxxx' #my  token here
    }});

    $tx->send({json => {
        profit_table => 1
    }});
});

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Answers

  • @sn1 you need to chain request, first you send authorize request and then check in $tx->on(json => sub { if msg_type of response is authorize then send profit_table request

     $tx->on(json => sub {
            my ($tx, $data) = @_;
            # check $data->{msg_type} eq 'authorize' then send `profit_table`
            say "Answer is: " . Dumper(\$data);
        });
    
Sign In or Register to comment.