Skip to content

Commit

Permalink
fix: better error handling and reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
d1snin committed Oct 16, 2024
1 parent 1c26b58 commit bfc948e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public suspend fun HttpClient.webSocketEvents(

withRetries(onError = {
logger.w {
"Error opening WS session: ${it.message}"

it.printStackTrace()
"Error handling WS session: ${it.message}"
}

it.printStackTrace()
}) {
webSocket(
urlString = url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dev.d1s.ktor.events.client

import io.ktor.client.plugins.websocket.*
import kotlinx.coroutines.coroutineScope
import org.lighthousegames.logging.KmLog
import org.lighthousegames.logging.logging

Expand All @@ -31,19 +32,15 @@ public suspend inline fun <reified T> DefaultClientWebSocketSession.receiveWebSo
receiveDeserialized<ClientWebSocketEvent<T>>()

/**
* Dequeues frames containing [ClientWebSocketEvent] and tries to deserialize it. Will retry if something went wrong while receiving a frame.
* Dequeues frames containing [ClientWebSocketEvent] and tries to deserialize it.
*
* @see webSocketEvents
*/
public suspend inline fun <reified T> DefaultClientWebSocketSession.receiveWebSocketEvents(crossinline receiver: suspend (ClientWebSocketEvent<T>) -> Unit) {
withRetries(continuous = true, onError = {
EventReceiverLog.w {
"Error receiving web socket events: ${it.message}"

it.printStackTrace()
coroutineScope {
while (true) {
val event = receiveWebSocketEvent<T>()
receiver(event)
}
}) {
val event = receiveWebSocketEvent<T>()
receiver(event)
}
}

0 comments on commit bfc948e

Please sign in to comment.