Async-Selector

 view release on metacpan or  search on metacpan

lib/Async/Selector/Example/Mojo.pod  view on Meta::CPAN

        get '/' => sub {
            my $self = shift;
            $self->render('index');
        };
    
        get '/comet' => sub {
            my $self = shift;
            my $client_sequence = $self->param('seq');
            my $watcher = $selector->watch(res => $client_sequence, sub {
                my ($w, %resources) = @_;
                my ($resource, $sequence)
                    = ($resources{res}{resource}, $resources{res}{sequence});
                $self->render_data("$sequence $resource");
                $w->cancel();
            });
            $self->on(finish => sub {
                $watcher->cancel();
            });
        };
    
        websocket '/websocket' => sub {
            my $self = shift;
            Mojo::IOLoop->stream($self->tx->connection)->timeout(0);
            my $watcher = $selector->watch(res => 0, sub {
                my ($w, %resources) = @_;
                my ($resource, $sequence)
                    = ($resources{res}{resource}, $resources{res}{sequence});
                $self->send("$sequence $resource");
            });
            $self->on(finish => sub {
                $watcher->cancel();
            });
        };
    
        app->start;
    }
    
    
    __DATA__
    @@ index.html.ep
    <!DOCTYPE html>
    <html>
      <head>
        <title>Async::Selector test</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script><!--
    $(function() {
        var RECONNECT_BACKOFF = 2000;
        // Setup Comet (long-polling)
        var my_sequence = 0;
        var sendCometRequest = function() {
            $.get("<%= url_for('comet') %>?seq=" + my_sequence)
                .done(function(data) {
                    data = data.split(" ");
                    my_sequence = data[0];
                    $('#comet_sequence').text(data[0]);
                    $('#comet_resource').text(data[1]);
                    sendCometRequest();
                })
                .fail(function() {
                    setTimeout(sendCometRequest, RECONNECT_BACKOFF);
                });
        };
        sendCometRequest();
    
        // Setup WebSocket
        var connectWebsocket = function() {
            var ws = new WebSocket("<%= url_for('websocket')->to_abs %>");
            ws.onmessage = function(event) {
                var data = event.data.split(" ");
                $('#websocket_sequence').text(data[0]);
                $('#websocket_resource').text(data[1]);
            };
            ws.onclose = function() {
                setTimeout(connectWebsocket, RECONNECT_BACKOFF);
            };
        };
        connectWebsocket();
    });
    //--></script>
      </head>
      <body>
        <div>
          <h1>Comet (long-polling)</h1>
          <p>Sequence number: <span id="comet_sequence"></span></p>
          <textarea id="comet_resource" rows="10" cols="100" readonly="true"></textarea>
        </div>
        <div>
          <h1>WebSocket</h1>
          <p>Sequence number: <span id="websocket_sequence"></span></p>
          <textarea id="websocket_resource" rows="10" cols="100" readonly="true"></textarea>
        </div>
      </body>
    </html>



=head1 USAGE

=over

=item 1.

Install L<Async::Selector> and L<Mojolicious>.

=item 2.

Save the code as C<mojo.pl> for example.

=item 3.

Run C<mojo.pl>.

    $ perl mojo.pl daemon


=item 4.

Access C<http://localhost:3000/> from Web browsers.

=item 5.

You will see two random sequences of characters displayed in textareas,
both of which represent the same resource.

One of the two sequences is updated periodically using Comet (long-polling),
while the other using WebSocket.

=back


=head1 DESCRIPTION

The code above consists of three parts, the resource part, the HTTP
part and the HTML/Javascript part.



( run in 1.960 second using v1.01-cache-2.11-cpan-39bf76dae61 )