https://kotlinlang.org logo
#ktor
Title
d

d.bellingroth

08/27/2019, 3:02 PM
Hey there, I'm trying to implement a websocket reverse proxy using KTOR. Does anybody have an idea how to do this? What I'm trying in the moment looks like this:
Copy code
webSocket("{path...}") {

        val url = baseUrl.replaceFirst("http", "ws") + "/" + call.parameters.getAll("path")?.joinToString("/")

        val sourceSession = this

        <http://client.ws|client.ws>({
            url {
                takeFrom(url)
                parameters.appendAll(call.request.queryParameters)
            }
        }) {
            val targetSession = this
            select<Unit> {
                targetSession.incoming.onReceive { frame ->
                    println("Received frame from target ${frame.data.toString(Charset.defaultCharset())}")
                    sourceSession.send(frame.copy())
                }
                sourceSession.incoming.onReceive { frame ->
                    println("Received frame from source ${frame.data.toString(Charset.defaultCharset())}")
                    targetSession.send(frame.copy())
                }
            }
        }
    }