Skip to content

Instantly share code, notes, and snippets.

@nailgilaziev
Created April 25, 2018 10:01
Show Gist options
  • Save nailgilaziev/4ead4fa43274949529396db6bd3b3b60 to your computer and use it in GitHub Desktop.
Save nailgilaziev/4ead4fa43274949529396db6bd3b3b60 to your computer and use it in GitHub Desktop.
Client for Echo UpperCase Server ktor
fun main(args: Array<String>) = runBlocking<Unit> {
val client = HttpClient(CIO.config {
maxConnectionsCount = 1000 // Maximum number of socket connections.
endpoint.apply {
maxConnectionsPerRoute = 100 // Maximum number of requests for a specific endpoint route.
pipelineMaxSize = 20 // Max number of opened endpoints.
keepAliveTime = 5000 // Max number of milliseconds to keep each connection alive.
connectTimeout = 5000 // Number of milliseconds to wait trying to connect to the server.
connectRetryAttempts = 5 // Maximum number of attempts for retrying a connection.
}
}).config {
install(WebSockets)
}
println("started")
// client.ws(method = HttpMethod.Get, host = "10.18.8.73", port = 8080, path = "/ws") { // this: WebSocketSession
// (this as DefaultWebSocketSession).apply {
// pingInterval = Duration.ofSeconds(15)
// timeout = Duration.ofSeconds(5)
// }
// var i = 1
// send(Frame.Text("Hi"))
// println("hi sended")
// incoming.mapNotNull { it as? Frame.Text }.consumeEach { message ->
// println(message.readText())
// outgoing.send(Frame.Text("Message #${i++} fired"))
// }
// }
val ws = client.webSocketSession(host = "10.18.8.73",port = 8080, path = "/ws")
try {
ws.pingInterval = Duration.ofSeconds(15)
ws.timeout = Duration.ofSeconds(5)
ws.send(Frame.Text("Hi"))
println("hi sended")
var i = 1
ws.incoming.mapNotNull { it as? Frame.Text }.consumeEach { message ->
println(message.readText())
delay(10000)
ws.outgoing.send(Frame.Text("Message #${i++} fired"))
}
}
finally {
ws.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment