[英]Simple Perl websocket client
我想在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
的文檔中所示,但是我收到了以下消息:
Can't locate object method "new" via package "Protocol::WebSocket" at ./webSocketClient.pl.
我做錯了什么?
Protocol::WebSocket
是WebSocket協議的低級實現。 它不包含發送/接收數據的代碼; 它只是解析協議消息。
您可能希望查看使用Protocol::WebSocket
和各種模塊的examples
,請參閱examples
。 在此模塊附帶的wsconsole
實用程序中實現了一個很好的客戶端示例。
CPAN上有幾個高級模塊實現了隱藏所有低級內容的WebSockets,其中大多數都使用Protocol::WebSocket
。 看一下AnyEvent::WebSocket::Client
或Net::Async::WebSocket::Client
。
示例代碼中存在錯誤。 Protocol::WebSocket->new
應該是Protocol::WebSocket::Client->new
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.