ktrampas has asked for the wisdom of the Perl Monks concerning the following question:
I'll try to shot as possible - do not waste your time.
I need each socket connection to have own id sent by javascript (client side) like ws://domain.com:port/USER_ID. Already two weeks on it. Pleae help me - bear guaranteed :)
My code as follow:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-12 +52"> <title>New Page 1</title> </head> <body> <p>#!/usr/local/bin/perl -w<br> use IO::Socket::INET;<br> use Digest::SHA1 qw(sha1);<br> use MIME::Base64;<br> use Protocol::WebSocket::Frame;<br> # auto-flush on socket<br> $| = 1;<br> # creating a listening socket<br> my $socket = new IO::Socket::INET (<br> LocalHost => '0.0.0.0',<br> LocalPort => '8080',<br> Proto => 'tcp',<br> Listen => 5,<br> Reuse => 1<br> );<br> die "cannot create socket $!\n" unless $socket;<br> print "server waiting for client connection on port 7777\n"; +<br> while(1){<br> if($socket->connected) {<br> #$socket->connect($port, $ip) or die $!;<br> print "connektintas \n";<br> }<br> # waiting for a new client connection<br> my $client_socket = $socket->accept();<br> my $digest;<br> # get information about a newly connected client<br> my $client_address = $client_socket->peerhost();<br> my $client_port = $client_socket->peerport();<br> print "connection from $client_address:$client_port\n";<br> # read up to 1024 characters from the connected client<br> my $data = "";<br> $client_socket->recv($data, 1024);<br> print "received data: $data\n";<br> if($data=~/WebSocket-Key\:\s+(.*?)\s+/gs){<br> $digest = encode_base64(sha1($1."258EAFA5-E914-47DA-95CA-C5AB0DC8 +5B11"));#magic stream<br> }<br> # write response data to the connected client<br> $client_socket->send(<br> "HTTP/1.1 101 Switching Protocols\r\n".<br> "Upgrade:websocket\r\n".<br> "Connection:Upgrade\r\n".<br> "Sec-WebSocket-Accept:".$digest."\r\n");<br> my $frame = Protocol::WebSocket::Frame->new('after connection sendi +ng');<br> $client_socket->send($frame->to_bytes);<br> # notify client that response has been sent<br> # shutdown($client_socket, 1);<br> }<br> $socket->close();</p> </body> </html>
2018-02-18 Athanasius added code and paragraph tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dear Monks, again socket (wbsocket server side)
by Mr. Muskrat (Canon) on Feb 14, 2018 at 16:31 UTC |