简体   繁体   中英

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 ?

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 .

There is an error in the example code. Protocol::WebSocket->new should be Protocol::WebSocket::Client->new

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM