Greetings and a merry christmas everyone,
I come to you tonight because I am encountering quit a hurdle in my understanding.

I recently started playing an online game which requires the player to code programs that interacts with a server api. After getting to know said API I discovered one could use websockets. So on I went, rummaging through my local CPAN distributor for the right module, trying this or that until I found AnyEvent::Websocket::Client.

This is my first time with event driven programming so please bear with my ignorance:
Update: From what I understand, once this is called, an event loop is entered and everything happens in the callback routine

I rewrote my code following this behaviour and it got working.
Now, what if I need to keep an eye on two event loops?
Do I need to spawn a new process and have it pipe data to me?
I am thinking about something along those lines:

from where I stand a possible solution would be:
1 Acting process that spawns two websocket listening children, each child openend with a handle so the father can use the diamond operator.

Now a real mindfuck: what design to choose from:
1) a shared variable referencing my local copy of the overall system state, which gets updated by both processes and periodically checked by their father? 2) a pipe system from the children to their father that feeds him new info?
#this code should block if nothing comes from child 2 and during that +time it #may receive data from child1 that wont be acted upon while(<$child1>){ try something...will block if nothing comes from child 1 my $line = <$child2> while(!defined $line){ $line = <$child2>; do something else } }
Old code
while(1){ print "recving\n"; my $hash = $trader->{venues}->{$venues[0]}->{ticker_tape}->recv; print "recved\n"; my $parsed = parse_json($hash); print "\n" x 5; print "$hash"; print "\n" x 5; }
from what I understand, this code should block on the line begining with
"my hash, such and such"
until it receives the signal from the following snippet:
$self->{venues}->{$i}->{client} = AnyEvent::WebSocket::Client->new +; my $cv = $self->{venues}->{$i}->{client}->connect("wss://api.s +tockfighter.io/ob/api/ws/$self->{account}/venues/$i/tickertape")->cb( +sub{ our $connection = eval { shift->recv }; if($@) { warn $@; return; } $connection->on(each_message => sub { my($connection, $message) = @_; my $decode = parse_json($message->{body}); print "got a message ticker!\n"; $self->{venues}->{$i}->{ticker_tape}->send($message +->{body}); }); $connection->on(finish => sub { # $connection is the same connection object my($connection) = @_; $connection->close; }); });
said signal containing a json string.
Thing is: I only receive ONE string, the message
"got a message ticker"
appears only once which indicates that the callback is only called the first time. then the message gets repeated over and over afterward. I think I am missing some important concept regarding this type of programming and I would be most grateful if someone could point it out.


In reply to AnyEvent Question by QuillMeantTen

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.