From 4d817fd01b3c78cb0c3c26d5beb262d2bba3b77f Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Tue, 2 Jun 2026 08:09:28 +0700 Subject: [PATCH] remove custom websocket --- .../kotlin/su/reya/coop/CoopWebSocket.kt | 75 ------------------- .../commonMain/kotlin/su/reya/coop/Nostr.kt | 8 +- 2 files changed, 3 insertions(+), 80 deletions(-) delete mode 100644 shared/src/commonMain/kotlin/su/reya/coop/CoopWebSocket.kt diff --git a/shared/src/commonMain/kotlin/su/reya/coop/CoopWebSocket.kt b/shared/src/commonMain/kotlin/su/reya/coop/CoopWebSocket.kt deleted file mode 100644 index ff2a658..0000000 --- a/shared/src/commonMain/kotlin/su/reya/coop/CoopWebSocket.kt +++ /dev/null @@ -1,75 +0,0 @@ -package su.reya.coop - -import io.ktor.client.HttpClient -import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession -import io.ktor.client.plugins.websocket.webSocketSession -import io.ktor.client.request.url -import io.ktor.websocket.Frame -import io.ktor.websocket.close -import io.ktor.websocket.readBytes -import io.ktor.websocket.readText -import kotlinx.coroutines.cancel -import kotlinx.coroutines.channels.ClosedReceiveChannelException -import rust.nostr.sdk.ConnectionMode -import rust.nostr.sdk.CustomWebSocketTransport -import rust.nostr.sdk.WebSocketAdapter -import rust.nostr.sdk.WebSocketAdapterWrapper -import rust.nostr.sdk.WebSocketMessage - -class KtorWebSocketAdapter( - private val client: HttpClient, - private val session: DefaultClientWebSocketSession -) : WebSocketAdapter { - - override suspend fun send(msg: WebSocketMessage) { - try { - when (msg) { - is WebSocketMessage.Text -> session.send(Frame.Text(msg.text)) - is WebSocketMessage.Binary -> session.send(Frame.Binary(true, msg.bytes)) - is WebSocketMessage.Ping -> session.send(Frame.Ping(msg.bytes)) - is WebSocketMessage.Pong -> session.send(Frame.Pong(msg.bytes)) - else -> {} - } - } catch (e: Exception) { - println("Attempted to send on a closed WebSocket: ${e.message}") - throw e - } - } - - override suspend fun recv(): WebSocketMessage? { - return try { - when (val frame = session.incoming.receive()) { - is Frame.Text -> WebSocketMessage.Text(frame.readText()) - is Frame.Binary -> WebSocketMessage.Binary(frame.readBytes()) - is Frame.Ping -> WebSocketMessage.Ping(frame.readBytes()) - is Frame.Pong -> WebSocketMessage.Pong(frame.readBytes()) - else -> null - } - } catch (e: ClosedReceiveChannelException) { - null - } catch (e: Exception) { - throw e - } - } - - override suspend fun closeConnection() { - session.cancel() - session.close() - } -} - -class CoopWebSocketClient(private val httpClient: HttpClient) : CustomWebSocketTransport { - override fun supportPing(): Boolean = false - - override suspend fun connect(url: String, mode: ConnectionMode): WebSocketAdapterWrapper { - try { - val session = httpClient.webSocketSession { - url(url) - } - val adapter = KtorWebSocketAdapter(httpClient, session) - return WebSocketAdapterWrapper(adapter) - } catch (e: Exception) { - throw e - } - } -} diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt index e098895..8bccc85 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt @@ -2,7 +2,6 @@ package su.reya.coop import io.ktor.client.HttpClient import io.ktor.client.call.body -import io.ktor.client.plugins.websocket.WebSockets import io.ktor.client.request.get import io.ktor.client.statement.HttpResponse import kotlinx.coroutines.Job @@ -106,17 +105,16 @@ class Nostr { // Initialize the logger for nostr client initLogger(LogLevel.DEBUG) + // Initialize the database and gossip instance val lmdb = NostrDatabase.lmdb(dbPath) val gossip = NostrGossip.inMemory() + + // Set the idle timeout for relays val idleTimeout = Duration.parse("5m") - val httpClient = HttpClient { - install(WebSockets) - } client = ClientBuilder() .signer(signer) - .websocketTransport(CoopWebSocketClient(httpClient)) .database(lmdb) .gossip(gossip) .gossipConfig(