问题
I'm trying to connect to a websocket with the following Perl code:
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;
However, I get the following error:
handshake error: Wrong response line at websocket.pl line 10.
How can I fix this error?
As an aside, what I'm trying to do is port the following working node.js code to 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();
I'm also reviewing a tutorial on Perl socket programming.
Any help either debugging it or advice on which package I should use to connect (other than AnyEvent::WebSocket::Client) would be helpful.
回答1:
The error messages looks like a SSL handshake problem. (websocket uses wss). Are the certificates trusted? You might add this:
# Ignore SSL verification issues
$client->{ssl_no_verify} = 1;
I tested your code against my websocket and it works. (no error message).
来源:https://stackoverflow.com/questions/26487175/handshake-error-wrong-response-line-when-connecting-to-websocket