我正在尝试使用以下Perl代码连接到WebSocket:
然而,我收到了以下错误:
作为旁注,我想做的是将以下可行的node.js代码移植到Perl中:
use AnyEvent;
use AnyEvent::WebSocket::Client 0.12;
my $client = AnyEvent::WebSocket::Client->new;
$client->connect("wss://example.com")->cb(sub {
my $connection = eval { shift->recv }; # This line is generating the error
if($@) {
# handle error...
warn $@;
return;
}
# send a message through the websocket...
$connection->send('');
# receive message from the websocket...
$connection->on(each_message => sub {
my($connection, $message) = @_;
print "Recieved Message...\n"
});
# handle a closed connection...
$connection->on(finish => sub {
# $connection is the same connection object
my($connection) = @_;
print "Disconnected...\n";
});
$connection->close;
});
AnyEvent->condvar->recv;
然而,我收到了以下错误:
handshake error: Wrong response line at websocket.pl line 10.
我该如何修复这个错误?
作为旁注,我想做的是将以下可行的node.js代码移植到Perl中:
var autobahn = require('autobahn');
var wsuri = "wss://example.com";
var connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
connection.onopen = function (session) {
function txtEvent (args,kwargs) {
console.log(args);
};
session.subscribe('textmsg', txtEvent);
}
connection.onclose = function () {
console.log("Connection closed");
}
connection.open();
我还在阅读有关Perl套接字编程的教程。
如果能提供调试帮助或建议使用哪个软件包进行连接(除了AnyEvent::WebSocket::Client
),将非常有帮助。