Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

  • Receive data from one websocket, depending on that data act upon it
  • Receive data from another websocket to learn how what I did changed the system state
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found