Closed ethought closed 8 years ago
Are you using the exact code that is in the synopsis? Did you intend on closing this issue?
I had an error in the copied code. But even now I cannot seem to get the client to connect to my websockets server.
Here is the code I am using:
package test;
use AnyEvent::WebSocket::Client;
sub new { my $class = shift; my $client = AnyEvent::WebSocket::Client->new; my $self = { 'client' => $client }; bless $self, $class; return $self; }
sub broadcastSidePrice { my ( $self, $jsonreturn ) = @;
my $client = $self->{client};
$client->connect("ws://192.168.1.50:3000/sideprices")->cb(
sub {
my $connection = eval { shift->recv };
if ($@) {
# handle error...
warn $@;
return;
}
print "debug: sending\n";
$connection->send($json_return);
$connection->close;
}
);
}
1;
What is the error you are getting? I get
unable to connect at foo.pl line 19.
though that is what i would expect given that I don't have a server at that URL.
No error, and no output in server console (using Mojolicious::Lite as server). The server works fine with other ws clients.
The code never seems to get to:
print "debug: sending\n";
Does the client script terminate?
No.
Using the below code it just prints "END"
#!/usr/bin/perl -w
use strict;
use test;
my $TEST = new test;
for(my $i = 0; $i < 300; $i++){
$TEST->broadcastSidePrice($i);
}
print "END\n";
Right, but after printing END it should exit (which is what I meant by terminate), unless you enter an event loop see this FAQ:
https://metacpan.org/pod/AnyEvent::FAQ#My-program-exits-before-doing-anything-whats-going-on
What you want is something like this:
#!/usr/bin/perl -w
use strict;
use test;
use AnyEvent;
my $TEST = new test;
for(my $i = 0; $i < 300; $i++){
$TEST->broadcastSidePrice($i);
}
print "END\n";
AnyEvent->condvar->recv;
this will put you in the event loop until you press ^C. You can have it exit normally by using the send
method on the $cv
when your processing is complete.
Hi - I am trying to get the basic client example working but running into this error:
Can't locate object method "recv" via package
on the following line:
our $connection = eval { shift->recv };
Any ideas how to fix this?
Thanks