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