From 832ce5fb4bf7ad92affa0386df000d9ac10b46b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz> Date: Thu, 3 Dec 2020 22:37:07 +0100 Subject: [PATCH] Aktualizace stavu vysilani pres websockets --- lib/PiTube.pm | 3 +++ lib/PiTube/Controller/Nginx.pm | 7 +++---- lib/PiTube/Controller/Stream.pm | 20 ++++++++++++++++++++ nginx.conf | 8 ++++++++ pi_tube.conf | 1 + 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/PiTube.pm b/lib/PiTube.pm index c9b64fd..863972e 100644 --- a/lib/PiTube.pm +++ b/lib/PiTube.pm @@ -30,6 +30,7 @@ sub startup { ; $pg->migrations->from_file($self->home . '/sql/migrations.sql'); $pg->migrations->migrate(); + $self->helper( pg => sub { return $pg; } ); # Spojeni s databazi my $schema = PiTube::Schema->connect($cfg->{database}); @@ -84,6 +85,8 @@ sub startup { # $r->put('/streams/:id')->over( is => 'administrator' )->to('Stream#update'); # $r->delete('/streams/:id')->over( is => 'administrator' )->to('Stream#delete'); + $r->websocket('/ws/streams')->to('Stream#ws'); + $r->get('/oauth2')->to('OAuth2#callback'); $r->get('/logout')->to('OAuth2#do_logout'); $r->post('/callback/rtmp')->to('Nginx#callback_rtmp'); diff --git a/lib/PiTube/Controller/Nginx.pm b/lib/PiTube/Controller/Nginx.pm index f84573d..789af4a 100644 --- a/lib/PiTube/Controller/Nginx.pm +++ b/lib/PiTube/Controller/Nginx.pm @@ -7,7 +7,6 @@ use constant HLS => qr/hls\/([a-z0-9\-]+)(_\w+)?(\/\w+)?\.(m3u8|ts)$/i; sub callback_rtmp { my $c = shift; my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg); - $pubsub = $pubsub->json('messages'); if ( $c->param('call') =~ /publish/ ) { my $name = $c->param('name'); @@ -38,10 +37,10 @@ sub callback_rtmp { is_live => ( $c->param('call') =~ /done/ ) ? 'f':'t', }); - $pubsub->notify( messages => { - call => $c->param('call'), + $pubsub->json('streams')->notify( streams => { + call => $c->param('call'), + stream_id => $stream->id, }); - } $c->render( status => 204, text => '' ); diff --git a/lib/PiTube/Controller/Stream.pm b/lib/PiTube/Controller/Stream.pm index 94d3903..db70a9d 100644 --- a/lib/PiTube/Controller/Stream.pm +++ b/lib/PiTube/Controller/Stream.pm @@ -1,6 +1,9 @@ package PiTube::Controller::Stream; + +use feature 'signatures'; use Mojo::Base 'Mojolicious::Controller'; use Mojo::UserAgent; +use Mojo::Pg::PubSub; use constant CONTENT_TYPE => { m3u8 => 'application/vnd.apple.mpegurl', @@ -175,6 +178,23 @@ sub recording { $c->render( json => { recording => $stream->recording } ); } +sub ws { + my $c = shift; + + $c->inactivity_timeout(300); + + my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg); + + $pubsub->listen(streams => sub($pubsub, $payload) { + $c->send($payload); + }); + + $c->on(finish => sub ($c, $code, $reason = undef) { + $pubsub->unlisten('streams'); + $c->app->log->debug("WebSocket closed with status $code"); + }); +} + 1; __END__ diff --git a/nginx.conf b/nginx.conf index eb42334..0460dd1 100644 --- a/nginx.conf +++ b/nginx.conf @@ -90,6 +90,14 @@ http { expires 24h; } + location /ws { + proxy_pass http://pitube:3000/ws; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + proxy_set_header Host $host; + } + location / { proxy_pass http://pitube:3000/; add_header Access-Control-Allow-Origin *; diff --git a/pi_tube.conf b/pi_tube.conf index 2f97f57..6ea5005 100644 --- a/pi_tube.conf +++ b/pi_tube.conf @@ -38,4 +38,5 @@ }, matomo_id => $ENV{MATOMO_ID} // 0, base_url => $ENV{BASE_URL}, + ws_url => $ENV{WS_URL}, }; -- GitLab