bliako has asked for the wisdom of the Perl Monks concerning the following question:
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'
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Connect to a *secure* websocket server (wss)
by Corion (Patriarch) on Sep 24, 2025 at 14:19 UTC | |
by bliako (Abbot) on Sep 24, 2025 at 14:51 UTC | |
Re: Connect to a *secure* websocket server (wss)
by cavac (Prior) on Sep 24, 2025 at 14:33 UTC | |
by bliako (Abbot) on Sep 24, 2025 at 14:54 UTC |