#!/usr/local/bin/perl -w
use IO::Socket::INET;
use Digest::SHA1 qw(sha1);
use MIME::Base64;
use Protocol::WebSocket::Frame;
# auto-flush on socket
$| = 1;
# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '0.0.0.0',
LocalPort => '8080',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7777\n";
while(1){
if($socket->connected) {
#$socket->connect($port, $ip) or die $!;
print "connektintas \n";
}
# waiting for a new client connection
my $client_socket = $socket->accept();
my $digest;
# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";
# read up to 1024 characters from the connected client
my $data = "";
$client_socket->recv($data, 1024);
print "received data: $data\n";
if($data=~/WebSocket-Key\:\s+(.*?)\s+/gs){
$digest = encode_base64(sha1($1."258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));#magic stream
}
# write response data to the connected client
$client_socket->send(
"HTTP/1.1 101 Switching Protocols\r\n".
"Upgrade:websocket\r\n".
"Connection:Upgrade\r\n".
"Sec-WebSocket-Accept:".$digest."\r\n");
my $frame = Protocol::WebSocket::Frame->new('after connection sending');
$client_socket->send($frame->to_bytes);
# notify client that response has been sent
# shutdown($client_socket, 1);
}
$socket->close();