简单的 Perl websocket 客户端 [英] Simple Perl websocket client
问题描述
我正在尝试用 Perl 编写一个简单的 websocket 客户端:
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;
如文档中所示代码>Protocol::WebSocket::Client,但我收到消息:
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.
我做错了什么?
推荐答案
Protocol::WebSocket
是 WebSocket的低级实现> 协议.它不包含发送/接收数据的代码;它只是解析协议消息.
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.
您可能想查看将 Protocol::WebSocket
与各种模块一起使用的示例,请参阅 examples
.一个很好的客户端示例在此模块附带的 wsconsole
实用程序中实现.
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.
CPAN 上有几个高级模块实现了隐藏所有低级内容的 WebSockets,其中大多数使用 Protocol::WebSocket
.看看 AnyEvent::WebSocket::Client
或 Net::Async::WebSocket::Client
.
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
.
这篇关于简单的 Perl websocket 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!