http://qs321.pair.com?node_id=1151141

QuillMeantTen has asked for the wisdom of the Perl Monks concerning the following question:

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.