我研究过 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/(.*)rn//) {
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 Protocolsrn";
print $client "Upgrade: websocketrn";
print $client "Connection: Upgradern";
print $client "Sec-WebSocket-Accept: $return_keyrn";
print $client "rn";
}
客户端/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*/ };
}
看起来 Perl 的sha1_base64
无法用=
填充。 最后的单个=
可能会起作用:
my $return_key = sha1_base64($key) . "=";
也许不同的sha1/base64实现会更好?