#!/usr/bin/env perl use utf8; use 5.10.2; use Net::WebSocket::Server; my $Running = 0; Net::WebSocket::Server->new( listen => 8080, tick_period => 1, on_tick => sub { my $server = shift; return unless $Running; $_->send_utf8("Running") for $server->connections; }, on_connect => sub { my ( $server, $connection ) = @_; $connection->on( ready => sub { my $connnection = shift; $connection->send_utf8("Connected… RUN|STOP"); }, utf8 => sub { my ( $connection, $message ) = @_; if ( uc $message eq "RUN" ) { $connection->send_utf8( $Running ? "Already running" : "Starting to run…"); $Running = 1; } elsif ( uc $message eq "STOP" ) { $connection->send_utf8($Running ? "Stopping…" : "Not running"); $Running = 0; } else { $connection->send_utf8("Unknown command: $message"); } }); }) ->start;