From f3e0f642492f91acea29e19d8251fa8ee741f7e1 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Fri, 28 Feb 2020 16:01:10 +0100 Subject: [PATCH] Improve Notification WS --- src/main/kotlin/fr/dcproject/Application.kt | 5 ++--- src/main/kotlin/fr/dcproject/routes/Notification.kt | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index b9f4596..cd00a73 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -182,8 +182,8 @@ fun Application.module(env: Env = PROD) { } install(WebSockets) { - pingPeriod = Duration.ofSeconds(5) // Disabled (null) by default - timeout = Duration.ofSeconds(3) + pingPeriod = Duration.ofSeconds(60) // Disabled (null) by default + timeout = Duration.ofSeconds(15) maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length. masking = false } @@ -204,7 +204,6 @@ fun Application.module(env: Env = PROD) { /* Declare publisher on event */ val publisher = Publisher(get(), get()) subscribe(EntityEvent.Type.UPDATE_ARTICLE.event) { - println("Article is updated ${it.target.id}") publisher.publish(it) } diff --git a/src/main/kotlin/fr/dcproject/routes/Notification.kt b/src/main/kotlin/fr/dcproject/routes/Notification.kt index 3bb2e64..8b9c710 100644 --- a/src/main/kotlin/fr/dcproject/routes/Notification.kt +++ b/src/main/kotlin/fr/dcproject/routes/Notification.kt @@ -21,10 +21,9 @@ import kotlinx.coroutines.launch fun Route.notificationArticle(redis: RedisAsyncCommands, client: HttpClient) { webSocket("/notifications") { val citizenId = call.citizen.id - - launch { + val job = launch { var score = 0.0 - while (true) { + while (!outgoing.isClosedForSend) { val result = redis.zrangebyscoreWithScores( "notification:$citizenId", Range.from( @@ -41,6 +40,7 @@ fun Route.notificationArticle(redis: RedisAsyncCommands, client: // TODO terminate coroutine after connection close ! } } + job.join() // TODO mark notification as read incoming.consumeAsFlow().mapNotNull { it as? Frame.Text }.collect {