Skip to content

Instantly share code, notes, and snippets.

@usmansaleem
Created December 6, 2017 07:40
Show Gist options
  • Save usmansaleem/dea6deeea6bec3d66141f0a5d106f86d to your computer and use it in GitHub Desktop.
Save usmansaleem/dea6deeea6bec3d66141f0a5d106f86d to your computer and use it in GitHub Desktop.
Vertx NetClient Example in Kotlin
package info.usmans.blog.example
import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.net.NetClientOptions
fun main(args: Array<String>) {
Vertx.vertx().deployVerticle(NetClientExample())
println("Running NetClientExample!")
}
/**
* A Vertx Verticle to connect to our custom websocket
*/
class NetClientExample : AbstractVerticle() {
override fun start() {
var options = NetClientOptions().apply {
isSsl = true
}
val client = vertx.createNetClient(options)
client.connect(8888, "localhost", { event ->
if(event.succeeded()) {
println("Connected")
val socket = event.result()
//send pass phrase ...
socket.write(System.getProperty("password"))
socket.handler({ data ->
println("Data received: ${data}")
//TODO: Do the work here ...
})
socket.closeHandler({
println("Socket closed")
//TODO: Attempt to reconnect? or give up
})
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment