"握手错误:错误的响应行"连接websocket时

时间:2014-10-21 12:51:05

标签: node.js perl websocket

我正在尝试使用以下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除外)都会有所帮助。

1 个答案:

答案 0 :(得分:0)

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

# Ignore SSL verification issues
$client->{ssl_no_verify} = 1;

我已根据我的websocket测试了您的代码,它可以正常工作。 (没有错误消息)。

相关问题