Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Dear Monks, again socket (wbsocket server side)

by ktrampas (Initiate)
on Feb 13, 2018 at 15:34 UTC ( #1209071=perlquestion: print w/replies, xml ) Need Help??
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 =&gt; '0.0.0.0',<br> LocalPort =&gt; '8080',<br> Proto =&gt; 'tcp',<br> Listen =&gt; 5,<br> Reuse =&gt; 1<br> );<br> die &quot;cannot create socket $!\n&quot; unless $socket;<br> print &quot;server waiting for client connection on port 7777\n&quot;; +<br> while(1){<br> if($socket-&gt;connected) {<br> #$socket-&gt;connect($port, $ip) or die $!;<br> print &quot;connektintas \n&quot;;<br> }<br> # waiting for a new client connection<br> my $client_socket = $socket-&gt;accept();<br> my $digest;<br> # get information about a newly connected client<br> my $client_address = $client_socket-&gt;peerhost();<br> my $client_port = $client_socket-&gt;peerport();<br> print &quot;connection from $client_address:$client_port\n&quot;;<br> # read up to 1024 characters from the connected client<br> my $data = &quot;&quot;;<br> $client_socket-&gt;recv($data, 1024);<br> print &quot;received data: $data\n&quot;;<br> if($data=~/WebSocket-Key\:\s+(.*?)\s+/gs){<br> $digest = encode_base64(sha1($1.&quot;258EAFA5-E914-47DA-95CA-C5AB0DC8 +5B11&quot;));#magic stream<br> }<br> # write response data to the connected client<br> $client_socket-&gt;send(<br> &quot;HTTP/1.1 101 Switching Protocols\r\n&quot;.<br> &quot;Upgrade:websocket\r\n&quot;.<br> &quot;Connection:Upgrade\r\n&quot;.<br> &quot;Sec-WebSocket-Accept:&quot;.$digest.&quot;\r\n&quot;);<br> my $frame = Protocol::WebSocket::Frame-&gt;new('after connection sendi +ng');<br> $client_socket-&gt;send($frame-&gt;to_bytes);<br> # notify client that response has been sent<br> # shutdown($client_socket, 1);<br> }<br> $socket-&gt;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

    You can't just put your Perl code inside of HTML and expect it to run.

    As a start, read all of the linked information at CGI Programming.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1209071]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-06-23 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (125 votes). Check out past polls.

    Notices?