我正在尝试用 Perl 编写一个简单的 websocket 客户端:
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 http://search.cpan.org/~vti/Protocol-WebSocket-0.18/lib/Protocol/WebSocket/Client.pm,
但我收到消息:
Can't locate object method "new" via package "Protocol::WebSocket" at ./webSocketClient.pl.
我做错了什么?
Protocol::WebSocket https://metacpan.org/pod/Protocol::WebSocket是一个低级实现WebSocket https://en.wikipedia.org/wiki/WebSocket协议。它不包含发送/接收数据的代码;它只是解析协议消息。
您可能想查看使用示例Protocol::WebSocket
具有各种模块,请参见examples
。一个很好的客户端示例是在wsconsole
该模块附带的实用程序。
CPAN 上有几个高级模块实现了隐藏所有低级内容的 WebSockets,其中大多数使用Protocol::WebSocket
。看一眼AnyEvent::WebSocket::Client https://metacpan.org/pod/AnyEvent::WebSocket::Client or Net::Async::WebSocket::Client https://metacpan.org/pod/Net::Async::WebSocket::Client.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)