Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Add streaming support
Browse files Browse the repository at this point in the history
  • Loading branch information
mdom committed Mar 19, 2016
1 parent c1bc618 commit 3245dde
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/App/txtnix/Cmd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,13 @@ subcmd
cmd => 'register',
comment => 'Register at your registry.';

subcmd
cmd => 'watch',
comment => 'Register at your registry.';

arg url => (
isa => 'Str',
comment => 'Websocket endpoint.',
);

1;
46 changes: 46 additions & 0 deletions lib/App/txtnix/Cmd/watch.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package App::txtnix::Cmd::watch;
use Mojo::Base 'App::txtnix';
use Mojo::JSON 'decode_json';
use App::txtnix::Tweet;
use App::txtnix::Source;

has 'url';

sub run {
my $self = shift;
$self->use_pager(0);
$self->ua->inactivity_timeout(0);
$self->ua->websocket(
$self->url => sub {
my ( $ua, $tx ) = @_;
say 'WebSocket handshake failed!' and return
unless $tx->is_websocket;
$tx->on(
finish => sub {
my ( $tx, $code, $reason ) = @_;
say "WebSocket closed with status $code.";
}
);
$tx->on(
message => sub {
my ( $tx, $msg ) = @_;
my $data = decode_json($msg);
return if !$data;
my $tweet = App::txtnix::Tweet->new(
timestamp => Mojo::Date->new( $data->{time} ),
source => App::txtnix::Source->new(
url => $data->{url},
nick => $data->{nick}
),
text => $data->{tweet},
);
$self->display_tweets( 1, $tweet );

}
);
}
);
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
}

1;

0 comments on commit 3245dde

Please sign in to comment.