“握手错误:错误的响应行"连接到 websocket 时 [英] "handshake error: Wrong response line" when connecting to websocket

查看:52
本文介绍了“握手错误:错误的响应行"连接到 websocket 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下 Perl 代码连接到 websocket:

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?

顺便说一句,我想要做的是将以下工作 node.js 代码移植到 Perl:

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();

我也在复习 Perl 套接字编程教程.

任何调试帮助或关于我应该使用哪个包进行连接的建议(除了 AnyEvent::WebSocket::Client) 会很有帮助.

Any help either debugging it or advice on which package I should use to connect (other than AnyEvent::WebSocket::Client) would be helpful.

推荐答案

错误消息看起来像是 SSL 握手问题.(websocket 使用 wss).证书可信吗?您可以添加以下内容:

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;

我在我的 websocket 上测试了你的代码并且它工作正常.(没有错误信息).

I tested your code against my websocket and it works. (no error message).

这篇关于“握手错误:错误的响应行"连接到 websocket 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆