Estimado Monjees

I am looking for guidance on how to connect to a server via secure websocket (wss) using perl, at as high level as possible, using a module preferably. My browser initiates this connection with this header Sec-WebSocket-Key: which is base64-encoded binary data. My interaction with it would be mostly reading what it periodically sends.

I have looked AnyEvent::WebSocket::Connection and Net::Async::WebSocket::Client but they do not mention wss. Am I wrong?

Whereas Mojo::UserAgent does support wss (it says), and allows for headers to be passed on but alas it fails. I used something like this:

use strict; use warnings; use Mojo::UserAgent; my $URI = 'wss://echo.websocket.org'; my $ua = Mojo::UserAgent->new; my $tx = $ua->build_websocket_tx($URI); $tx->req->headers->user_agent('Mozilla/5.0 (X11; Linux x86_64) AppleWe +bKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36'); print $tx->req->headers->to_string."\n"; $ua->start( $tx => sub { my ($ua, $tx) = @_; print "Headers: ".$tx->req->headers->to_string."\n"; print "WebSocket handshake failed!\n" and return unless $tx->is_websocket ; print "Got a handshake\n"; $tx->on(message => sub { my ($ua, $msg) = @_; print "WebSocket message: $msg\n"; $tx->finish; }); } ); print "waiting ...\n";

outputs this:

waiting ... UA: Mojo::UserAgent TX: Mojo::Transaction::HTTP WebSocket handshake failed!

I am using a test site for the target (https://ws-playground.netlify.app/) which works on the browser and I have checked it does not need extra headers/cookies. But above code fails.

10min Edit: this works (nodejs client):

wscat -c wss://echo.websocket.org -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36'

this does not (waits...) :

curl -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36' 'wss://echo.websocket.org'

In reply to Connect to a *secure* websocket server (wss) by bliako

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.