Have a look at https://metacpan.org/pod/Net::Async::WebSocket
For client: https://metacpan.org/pod/Net::Async::WebSocket::Client On Thu, 4 Feb 2021 at 10:32 AM, Wesley Peng <wes...@pengfamily.de> wrote: > I am trying to find a websocket client for perl. > But most modules on cpan is asynchronous, such as > AnyEvent::WebSocket::Client. > Do you know if there is a synchronous websocket client library available? > > Thanks in advance. > > > BTW, I tried to use the anyevent module above to access my WS service, it > doesn't work. > When program run, it just hangs without any output. > Can you see why this happens? > The code as below: > > use strict; > > use JSON; > > use Data::Dumper; > > use AnyEvent::WebSocket::Client 0.12; > > > > my $client = AnyEvent::WebSocket::Client->new; > > my $url = > 'ws://localhost:8080/ws/v2/producer/persistent/public/default/my-topic'; > > > > > $client->connect($url)->cb(sub { > > > > # make $connection an our variable rather than > > # my so that it will stick around. Once the > > # connection falls out of scope any callbacks > > # tied to it will be destroyed. > > our $connection = eval { shift->recv }; > > if($@) { > > # handle error... > > warn $@; > > return; > > } > > > > # send a message through the websocket... > > my $payload = encode_json({'payload' => 'hello world'}); > > $connection->send($payload); > > > > # recieve message from the websocket... > > $connection->on(each_message => sub { > > # $connection is the same connection object > > # $message isa AnyEvent::WebSocket::Message > > my($connection, $message) = @_; > > print Dumper decode_json($message); > > }); > > > > # handle a closed connection... > > # $connection->on(finish => sub { > > # # $connection is the same connection object > > # my($connection) = @_; > > # ... > > # }); > > > > # close the connection (either inside or > > # outside another callback) > > # $connection->close; > > > > }); > > > > ## uncomment to enter the event loop before exiting. > > ## Note that calling recv on a condition variable before > > ## it has been triggered does not work on all event loops > > AnyEvent->condvar->recv; > >