01 #!/usr/local/bin/perl -w 02 use strict; 03 use Mojolicious::Lite; 04 use Mojo::IOLoop; 05 06 my $listen = "http://localhost:8083"; 07 @ARGV = (qw(daemon --listen), $listen); 08 09 my $loop = Mojo::IOLoop->singleton(); 10 11 ########################################### 12 websocket "/myws" => sub { 13 ########################################### 14 my($self) = @_; 15 16 my $timer_cb; 17 my $counter = 10; 18 19 $timer_cb = sub { 20 $self->send_message( "$counter" ); 21 if( $counter-- > 0 ) { 22 $loop->timer(rand(1), $timer_cb); 23 } else { 24 $self->send_message( "BOOM!" ); 25 } 26 }; 27 28 $timer_cb->(); 29 }; 30 31 ########################################### 32 get '/' => sub { 33 ########################################### 34 my ($self) = @_; 35 36 ( my $ws_url = $listen ) =~ s/^http/ws/; 37 $ws_url .= "/myws"; 38 $self->{stash}->{ws_url} = $ws_url; 39 } => 'index'; 40 41 app->start(); 42 43 __DATA__ 44 @@ index.html.ep 45 % layout 'default'; 46 Random counter: 47 48 49 @@ layouts/default.html.ep 50 51 Random Countdown 52 60 61 <%== content %> 62