Improve Notification WS

This commit is contained in:
2020-02-28 16:01:10 +01:00
parent d34ae52522
commit f3e0f64249
2 changed files with 5 additions and 6 deletions

View File

@@ -182,8 +182,8 @@ fun Application.module(env: Env = PROD) {
} }
install(WebSockets) { install(WebSockets) {
pingPeriod = Duration.ofSeconds(5) // Disabled (null) by default pingPeriod = Duration.ofSeconds(60) // Disabled (null) by default
timeout = Duration.ofSeconds(3) timeout = Duration.ofSeconds(15)
maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length. maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length.
masking = false masking = false
} }
@@ -204,7 +204,6 @@ fun Application.module(env: Env = PROD) {
/* Declare publisher on event */ /* Declare publisher on event */
val publisher = Publisher(get(), get()) val publisher = Publisher(get(), get())
subscribe(EntityEvent.Type.UPDATE_ARTICLE.event) { subscribe(EntityEvent.Type.UPDATE_ARTICLE.event) {
println("Article is updated ${it.target.id}")
publisher.publish(it) publisher.publish(it)
} }

View File

@@ -21,10 +21,9 @@ import kotlinx.coroutines.launch
fun Route.notificationArticle(redis: RedisAsyncCommands<String, String>, client: HttpClient) { fun Route.notificationArticle(redis: RedisAsyncCommands<String, String>, client: HttpClient) {
webSocket("/notifications") { webSocket("/notifications") {
val citizenId = call.citizen.id val citizenId = call.citizen.id
val job = launch {
launch {
var score = 0.0 var score = 0.0
while (true) { while (!outgoing.isClosedForSend) {
val result = redis.zrangebyscoreWithScores( val result = redis.zrangebyscoreWithScores(
"notification:$citizenId", "notification:$citizenId",
Range.from( Range.from(
@@ -41,6 +40,7 @@ fun Route.notificationArticle(redis: RedisAsyncCommands<String, String>, client:
// TODO terminate coroutine after connection close ! // TODO terminate coroutine after connection close !
} }
} }
job.join()
// TODO mark notification as read // TODO mark notification as read
incoming.consumeAsFlow().mapNotNull { it as? Frame.Text }.collect { incoming.consumeAsFlow().mapNotNull { it as? Frame.Text }.collect {