我正在尝试使用以下Perl代码连接到websocket:
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();
我也在评论一个tutorial on Perl socket programming。
任何帮助调试它或建议我应该使用哪个包连接(除了AnyEvent::WebSocket::Client
)将有所帮助。
错误消息看起来像SSL握手问题。 (websocket使用wss)。证书是否值得信赖?你可以添加这个:
# Ignore SSL verification issues
$client->{ssl_no_verify} = 1;
我针对我的websocket测试了你的代码并且它有效。 (没有错误信息)。