Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Perl websocket client

I am trying to write a simple websocket client in Perl:

use Protocol::WebSocket::Client;

my $client = Protocol::WebSocket->new(url => 'ws://myserver:port');

# Sends a correct handshake header
$client->connect;

# Register on connect handler
$client->on(
    connect => sub {
        $client->write('hi there');
    }
);

# Parses incoming data and on every frame calls on_read
$client->read($reply);
print "$reply\n";

# Sends correct close header
$client->disconnect;

as shown in the documentation for Protocol::WebSocket::Client, but I receive the message:

Can't locate object method "new" via package "Protocol::WebSocket" at ./webSocketClient.pl.

What do I do wrong ?

like image 937
Francois Corthay Avatar asked Oct 16 '25 02:10

Francois Corthay


1 Answers

Protocol::WebSocket is a low-level implementation of WebSocket protocol. It doesn't contain the code that sends/receives data; it just parses the protocol messages.

You might want to look into examples for using Protocol::WebSocket with various modules, see examples. A good client example is implemented in the wsconsole utility which comes with this module.

There are several high-level modules on CPAN that implement WebSockets hiding all low level stuff, and most of them use Protocol::WebSocket. Take a look at AnyEvent::WebSocket::Client or Net::Async::WebSocket::Client.

like image 149
vti Avatar answered Oct 18 '25 23:10

vti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!