Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 89 lines (77 sloc) 2.061 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
package Chat;
use Squatting;

# This is a port of eg/chat-ajax-push.pl from the Continuity distribution.
# We're using the exact same JavaScript, but we've switched the server side
# with a Squatting implementation. Let's see if they can taste the difference.

package Chat::Controllers;
use selfvars;

our @messages;
our $got_message;

our @C = (

  C(
    Home => [ '/' ],
    get => sub {
      $self->render('home');
    },
  ),

  C(
    PushStream => [ '/pushstream/' ],
    get => sub {
      my $cr = $self->cr;
      my $w = Coro::Event->var(var => \$got_message, poll => 'w');
      while (1) {
        print STDERR "**** GOT MESSAGE, SENDING ****\n";
        my $log = join("<br>", @messages);
        $cr->print($log);
        $cr->next;
        print STDERR "**** Waiting for got_message indicator ****\n";
        $w->next;
      }
    },
    continuity => 1,
  ),

  C(
    SendMessage => [ '/sendmessage/' ],
    post => sub {
      my $input = $self->input;
      my $msg = $input->{message};
      my $name = $input->{username};
      if ($msg) {
        unshift @messages, "$name: $msg";
        pop @messages if $#messages > 15;
      }
      $got_message = 1;
      "Got it!";
    },
  ),

);

package Chat::Views;
use selfvars;
use HTML::AsSubs;

our @V = (
  V(
    'html',
    home => sub {
      my ($v) = @args;
      html(
        head(
          title('Chat'),
          script({ src => 'jquery.js' }),
          script({ src => 'chat-ajax-push.js' }),
        ),
        body(
          form({ id=> 'f', method => 'get', action => R('SendMessage') },
            div(
              input({ type => 'text', id => 'username', name => 'username', size => 10 }),
              input({ type => 'text', id => 'message', name => 'message', size => 50 }),
              input({ type => 'submit', id => 'sendbutton', name => 'sendbutton', value => 'Send', }),
              b({ id => 'status' }, "?")
            ),
            div({ id => 'log' }, '-- no messages yet --')
          )
        )
      )->as_HTML;
    }
  )
);

1;
Something went wrong with that request. Please try again.