“handshake error: Wrong response line” when connecting to websocket

北城以北 提交于 2021-02-07 18:20:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!