#!/usr/bin/perl use strict; use Socket; use Carp; sub logmsg { print "@_ \n" } my $port = shift || 8888; my $proto = getprotobyname('tcp'); ($port) = $port =~ /^(\d+)$/ or die "invalid port"; socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt:$!"; bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; listen(Server,SOMAXCONN) || die "listen: $!"; my $paddr; $SIG{CHLD} = \&REAPER; for ( ; $paddr = accept(Client,Server); close Client) { my($port,$iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr,AF_INET); my ($len, $data) = (0, ""); $len = sysread(Client, $data, 8192, length($data)); logmsg $data; my $Key; my $SecretKey = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; if ($data =~ /Sec-WebSocket-Key: (.*?)\r/) { my $data = $1.$SecretKey; use Digest::SHA1 qw/sha1_base64/; $Key = sha1_base64($data); } my $SendHandshake = "HTTP/1.1 101 Switching Protocols\r\n". "Upgrade: websocket\r\n". "Connection: Upgrade\r\n". "Sec-WebSocket-Accept: $Key\r\n\r\n"; logmsg $SendHandshake; print Client $SendHandshake; }___________________________________________________________________________________ <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> </head> <body> <script type="text/javascript"> </script> <table border="0" cellpadding="2" cellspacing="2" width="100%"> <tr> <td align="center"> <script type="text/javascript"> function WebSocketTest() { if ("WebSocket" in window) { alert("WebSocket is supported by your Browser!"); // Let us open a web socket var ws = new WebSocket("ws://localhost:8888"); ws.onopen = function() { //Web Socket is connected, send data using send() alert("Message is sent..."); ws.send("Message to send"); }; //alert(ws.readyState); //ws.send('meaasge'); ws.onmessage = function (evt) { var received_msg = evt.data; alert("Message is received..."); }; ws.onclose = function() { //websocket is closed. alert("Connection is closed..."); }; } else { // The browser doesn't support WebSocket alert("WebSocket NOT supported by your Browser!"); } } WebSocketTest(); </script> </td> </tr> </table> </body> </html> Событие onopen в javascript не срабатывает. Хендшейки пробовал по разному формировать, но не добился результата. В чем моя ошибка?
|