From 7921d4b78d8516579bd0007a1b0ac50398f2ac5a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Thu, 17 Feb 2022 01:40:00 +0100
Subject: [PATCH] Napojeni websocketu na nove pocitadlo

---
 lib/CF/Controller/Websockets.pm | 14 ++++++++++++++
 openapi.yaml                    |  2 +-
 tools/online-counter/online.go  | 13 +++++++------
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/lib/CF/Controller/Websockets.pm b/lib/CF/Controller/Websockets.pm
index c3c1685..6372c18 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 9290b50..2962988 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 7593d6b..69a5ab5 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)
-- 
GitLab