Created
December 28, 2020 17:56
-
-
Save yschimke/9b5d36790478a37776c97b7a73331578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S kotlinc-jvm -nowarn -script | |
@file:Repository("https://jcenter.bintray.com") | |
@file:Repository("https://oss.jfrog.org/oss-snapshot-local") | |
@file:DependsOn("io.rsocket.kotlin:rsocket-transport-ktor-client-jvm:0.13.0-SNAPSHOT") | |
@file:DependsOn("io.ktor:ktor-client-cio-jvm:1.5.0") | |
@file:CompilerOptions("-jvm-target", "1.8") | |
import io.ktor.client.* | |
import io.ktor.network.selector.* | |
import io.ktor.network.sockets.* | |
import io.rsocket.kotlin.ConnectionAcceptorContext | |
import io.rsocket.kotlin.RSocket | |
import io.rsocket.kotlin.core.RSocketConnector | |
import io.rsocket.kotlin.core.RSocketServer | |
import io.rsocket.kotlin.logging.LoggingLevel | |
import io.rsocket.kotlin.logging.PrintLogger | |
import io.rsocket.kotlin.payload.PayloadMimeType | |
import io.rsocket.kotlin.transport.ktor.client.rSocket | |
import io.rsocket.kotlin.transport.ktor.serverTransport | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.runBlocking | |
import io.ktor.client.engine.cio.CIO as ClientCIO | |
import io.ktor.client.features.websocket.WebSockets as ClientWebSockets | |
import io.rsocket.kotlin.transport.ktor.client.RSocketSupport as ClientRSocketSupport | |
suspend fun connectWebsocket(mimeType: PayloadMimeType, url: String): RSocket { | |
val httpClient = HttpClient(ClientCIO) { | |
install(ClientWebSockets) | |
install(ClientRSocketSupport) { | |
connector = RSocketConnector { | |
loggerFactory = PrintLogger.withLevel(LoggingLevel.DEBUG) | |
connectionConfig { | |
payloadMimeType = mimeType | |
} | |
} | |
} | |
} | |
return httpClient.rSocket(url) | |
} | |
suspend fun runTcpProxy(port: Int = 9000, acceptor: suspend ConnectionAcceptorContext.() -> RSocket) { | |
ActorSelectorManager(Dispatchers.IO).use { selector -> | |
val transport = aSocket(selector).tcp().serverTransport(port = port) | |
RSocketServer().bind(transport) { | |
acceptor() | |
}.join() | |
} | |
} | |
runBlocking { | |
println("running at tcp://localhost:9000") | |
runTcpProxy(port = 9000) { | |
val url = config.setupPayload.data.readText() | |
println("route: $url") | |
connectWebsocket(config.payloadMimeType, url) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment