连接websocket时出现"握手错误:响应行错误"。

4
我正在尝试使用以下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();

我还在阅读有关Perl套接字编程的教程

如果能提供调试帮助或建议使用哪个软件包进行连接(除了AnyEvent::WebSocket::Client),将非常有帮助。


AutobahnJS是WAMP(基于WebSocket)的一种实现。WAMP-over-WebSocket要求客户端在握手时宣布“wamp.2.json”(或其变体)作为WebSocket子协议。当您的客户端没有这样做时,符合规范的WAMP服务器将拒绝WebSocket连接。也许这就是发生的事情,Perl会输出无用/不正确的“错误响应”错误消息。 - oberstet
是的,这就是为什么,可悲的是我不知道如何修复它,我稍后会阅读相关资料,现在只能使用js。 - Banned_User
我也遇到了类似的错误:“无法通过包定位对象方法”recv“...” - someuser
1个回答

0
错误信息看起来像是SSL握手问题。(websocket使用wss)。 证书是否受信任? 您可以添加以下内容:
# Ignore SSL verification issues
$client->{ssl_no_verify} = 1;

我已经测试了您的代码并在我的 WebSocket 上运行,没有出现任何错误信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接