Skip to content
Snippets Groups Projects
Verified Commit 0bcd30f8 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Better counting

parent 181b62cd
Branches
Tags
No related merge requests found
Pipeline #6221 passed
...@@ -2,7 +2,7 @@ image: docker:20.10.9 ...@@ -2,7 +2,7 @@ image: docker:20.10.9
variables: variables:
DOCKER_TLS_CERTDIR: "/certs" DOCKER_TLS_CERTDIR: "/certs"
IMAGE_VER: 2.2.0 IMAGE_VER: 2.2.1
services: services:
- docker:20.10.9-dind - docker:20.10.9-dind
......
...@@ -28,12 +28,16 @@ sub main { ...@@ -28,12 +28,16 @@ sub main {
$c->on(json => sub( $c, $message ) { $c->on(json => sub( $c, $message ) {
if ( $message->{stream} ) { if ( $message->{stream} ) {
$c->redis->db->set( $c->redis->db->set(
join (':', ('live', $message->{stream}, $key)), join (':', ('live', $message->{stream}, $key)),
'live', 'EX', 16 'live', 'EX', 16
); );
my $keys = $c->redis->db->keys( 'live:' . $message->{stream} . ':*' );
$c->send(encode_json({ watchers => scalar @{$keys} })); $c->redis->db->keys( 'live:' . $message->{stream} . ':*', sub {
my ($db, $err, $res) = @_ ;
$c->send(encode_json({ watchers => scalar @{ $res} }));
});
} }
}); });
......
...@@ -3,23 +3,17 @@ ...@@ -3,23 +3,17 @@
%= include 'includes/player', src => $url, live => 1; %= include 'includes/player', src => $url, live => 1;
<script> <script>
var timer;
var ws;
function connect() { function connect() {
var ws = new WebSocket('<%= $c->config->{ws_url} %>'); ws = new WebSocket('<%= $c->config->{ws_url} %>');
ws.onmessage = function (event) { ws.onmessage = function (event) {
var data = JSON.parse(event.data) var data = JSON.parse(event.data)
if ( typeof data.watchers !== 'undefined' ) { if ( typeof data.watchers !== 'undefined' ) {
console.log(data); console.log(data);
} }
else {
ws.send(JSON.stringify({
event: "watch",
stream: "<%= $c->stash->{stream}{id} %>"
}));
}
}; };
ws.onclose = function(e) { ws.onclose = function(e) {
...@@ -35,6 +29,18 @@ function connect() { ...@@ -35,6 +29,18 @@ function connect() {
}; };
} }
function poll() {
timer = setInterval(function() {
ws.send(JSON.stringify({
event: "watch",
stream: "<%= $c->stash->{stream}{id} %>"
}));
}, 15000);
}
connect(); connect();
poll();
</script> </script>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment