#!/usr/bin/env perl use 5.10.2; use utf8; use strictures; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; my $TX; # This is what has me thinking this is hacky/kludgy. my $conn = $ua->websocket("ws://localhost:8080/" => sub { my $ua = shift; $TX = shift; say "WebSocket handshake failed!" and return unless $TX->is_websocket; $TX->on(finish => sub { my ( $tx, $code, $reason ) = @_; say "WebSocket closed with status $code."; exit; }); $TX->on(text => sub { my $tx = shift; say "Server > ", +shift; }); }); my @command = qw/ RUN STOP /; Mojo::IOLoop->recurring(5 => sub { my $command = $command[rand@command]; say "Client > $command"; $TX->send($command); }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;