0

我研究了 RFC 6455,不知道为什么与服务器握手失败。我还观察到与 Firefox 附加组件“HTTP Live Headers”的通信,一切看起来都很好......

我已经在 FireFox 中对此进行了测试。

服务器/Perl:

use IO::Socket::INET; 
use Digest::SHA1 qw(sha1_base64); 

$| = 1;

my $sock = IO::Socket::INET->new(LocalPort=>6060, Listen=>1, ReuseAddr=>1); 

while(my $client = $sock->accept) {

my $key = undef;
# I connect from localhost to localhost, 
# reading all with one sysread call definitely works in my scenario.
sysread $client, my $buf, 10000;

    while($buf =~s/(.*)\r\n//) {
        my $line = $1; 
        print "line='$line'\n";
        if($line =~/^Sec\-WebSocket\-Key:\s+(.*)$/i) {
            $key = $1; 
         }
     }

     $key .= '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
     my $return_key = sha1_base64($key);
     print $client "HTTP/1.1 101 Switching Protocols\r\n"; 
     print $client "Upgrade: websocket\r\n"; 
     print $client "Connection: Upgrade\r\n";
     print $client "Sec-WebSocket-Accept: $return_key\r\n"; 
     print $client "\r\n";
}

客户端/JavaScript

if("WebSocket" in window ) {
    sock = new WebSocket("ws://localhost:6060"); 
    sock.onopen = function() { /*this never fires*/ };
    sock.onerror = function() { /*the problem: this always fires*/ };
}
4

1 回答 1

0

看起来 Perlsha1_base64无法使用=. 最后一个=可能会起作用:

my $return_key = sha1_base64($key) . "=";

也许不同的 sha1/base64 实现会更好?

于 2013-09-20T19:58:32.017 回答