Skip to content

Commit

Permalink
Implement websocket keepalive
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Jan 23, 2025
1 parent fc60fee commit a5b5f30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
10 changes: 2 additions & 8 deletions src/main/kotlin/io/xconn/xconn/Client.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package io.xconn.xconn

import io.xconn.wampproto.auth.AnonymousAuthenticator
import io.xconn.wampproto.auth.ClientAuthenticator
import io.xconn.wampproto.serializers.JSONSerializer
import io.xconn.wampproto.serializers.Serializer

class Client(
private var authenticator: ClientAuthenticator = AnonymousAuthenticator(""),
private var serializer: Serializer = JSONSerializer(),
private var config: ClientConfig,
) {
suspend fun connect(url: String, realm: String): Session {
val joiner = WAMPSessionJoiner(authenticator, serializer)
val joiner = WAMPSessionJoiner(config)
val baseSession: BaseSession = joiner.join(url, realm)

return Session(baseSession)
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/io/xconn/xconn/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package io.xconn.xconn
import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession
import io.ktor.websocket.close
import io.xconn.wampproto.SessionDetails
import io.xconn.wampproto.auth.AnonymousAuthenticator
import io.xconn.wampproto.auth.ClientAuthenticator
import io.xconn.wampproto.messages.Message
import io.xconn.wampproto.serializers.JSONSerializer
import io.xconn.wampproto.serializers.Serializer
import kotlinx.coroutines.CompletableDeferred

Expand Down Expand Up @@ -118,3 +121,9 @@ data class UnsubscribeRequest(
val completable: CompletableDeferred<Unit>,
val subscriptionID: Long,
)

data class ClientConfig(
var authenticator: ClientAuthenticator = AnonymousAuthenticator(""),
var serializer: Serializer = JSONSerializer(),
val keepAliveInterval: Long = 0,
)
17 changes: 7 additions & 10 deletions src/main/kotlin/io/xconn/xconn/WAMPSessionJoiner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,27 @@ import io.ktor.websocket.Frame
import io.ktor.websocket.readBytes
import io.ktor.websocket.readText
import io.xconn.wampproto.Joiner
import io.xconn.wampproto.auth.AnonymousAuthenticator
import io.xconn.wampproto.auth.ClientAuthenticator
import io.xconn.wampproto.serializers.JSONSerializer
import io.xconn.wampproto.serializers.Serializer
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope

class WAMPSessionJoiner(
private val authenticator: ClientAuthenticator = AnonymousAuthenticator(""),
private val serializer: Serializer = JSONSerializer(),
private val config: ClientConfig,
) {
private val subProtocol = getSubProtocol(serializer)
private val subProtocol = getSubProtocol(config.serializer)
private val client =
HttpClient(CIO) {
install(WebSockets)
install(WebSockets) {
pingInterval = config.keepAliveInterval
}
defaultRequest {
header("Sec-WebSocket-Protocol", subProtocol)
}
}

suspend fun join(url: String, realm: String): BaseSession {
val welcomeCompleter = CompletableDeferred<BaseSession>()
val joiner = Joiner(realm, serializer, authenticator)
val joiner = Joiner(realm, config.serializer, config.authenticator)

val session = client.webSocketSession(url)

Expand All @@ -51,7 +48,7 @@ class WAMPSessionJoiner(

if (toSend == null) {
// Complete handshake and session creation
welcomeCompleter.complete(BaseSession(session, joiner.getSessionDetails(), serializer))
welcomeCompleter.complete(BaseSession(session, joiner.getSessionDetails(), config.serializer))
break
} else {
session.sendFrame(toSend)
Expand Down

0 comments on commit a5b5f30

Please sign in to comment.