我在FireFox中测试了这个。
Server / 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*/ };
}
答案 0 :(得分:0)
看起来Perl的sha1_base64
无法填充=
。最后一个=
可能会起作用:
my $return_key = sha1_base64($key) . "=";
也许不同的sha1 / base64实现会更好?