uperl / AnyEvent-WebSocket-Client

WebSocket client for AnyEvent
9 stars 10 forks source link

removed #21

Closed ethought closed 8 years ago

ethought commented 8 years ago

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

plicease commented 8 years ago

Are you using the exact code that is in the synopsis? Did you intend on closing this issue?

ethought commented 8 years ago

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;

plicease commented 8 years ago

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.

ethought commented 8 years ago

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";

plicease commented 8 years ago

Does the client script terminate?

ethought commented 8 years ago

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";
plicease commented 8 years ago

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.