diff --git a/lib/CF/Controller/Websockets.pm b/lib/CF/Controller/Websockets.pm index c3c1685842f458a2f2c31b09f8758e677f66e3e6..6372c181b2c15beb7847a8f46de5ee5fd835434f 100644 --- a/lib/CF/Controller/Websockets.pm +++ b/lib/CF/Controller/Websockets.pm @@ -24,6 +24,19 @@ sub main { $c->send(decode("UTF-8", $payload)); }); + my $listener_online = $pubsub->listen('online:0' => sub($pubsub, $payload) { #TODO: event_id + my @counts = split ' ', $payload; + $c->send(json => { + event => 'online_users_updated', + payload => { + all => $counts[0], + members => $counts[1], + group_size_full => $counts[2], + group_size_half => $counts[3], + } + }); + }); + $c->on(json => sub( $c, $message ) { if ( $message->{event} eq 'KEEPALIVE' ) { @@ -66,6 +79,7 @@ sub main { $c->on(finish => sub ($c, $code, $reason = undef) { $pubsub->unlisten('notify', $listener); + $pubsub->unlisten('online:0', $listener_online); #TODO: event_id $c->app->log->debug("WebSocket closed with status $code"); }); } diff --git a/openapi.yaml b/openapi.yaml index 9290b508f6eba9d66044e91efe11845b30bf4469..2962988085a127503ce1e372f31330395030de8f 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -1,7 +1,7 @@ openapi: 3.0.3 info: - version: "3.0.1" + version: "3.1.0" title: CF Online description: CF Online license: diff --git a/tools/online-counter/online.go b/tools/online-counter/online.go index 7593d6be3c7c1a92b9bd77ec62f85568b1e55792..69a5ab59da69a05d116fe8f8b9999e5ba5da446d 100644 --- a/tools/online-counter/online.go +++ b/tools/online-counter/online.go @@ -38,7 +38,7 @@ func main() { } }() - lastPub := make(map[string]time.Time) + lastPub := make(map[string]time.Time) // main loop ticker := time.Tick(time.Second * 1) @@ -73,16 +73,17 @@ func main() { // publsh counts for event, count := range online { - elapsed := now.Sub(lastPub[event]).Round(time.Second).Seconds() + elapsed := now.Sub(lastPub[event]).Round(time.Second).Seconds() - if elapsed < float64(count.All)/tarpitFactor { - break - } + if elapsed < float64(count.All)/tarpitFactor { + break + } - lastPub[event] = now + lastPub[event] = now topic := fmt.Sprintf("online:%s", event) message := fmt.Sprintf("%v", count) + message = message[1 : len(message)-1] if os.Getenv("DEBUG") != "" { fmt.Println(topic, message)