!!! This project is currently in an incubating phase. API surface, functionality and tool set might change. !!!
Koms is a TCP socket based communication library written in Kotlin and setup to be a Kotlin multiplatform library.
The main focus is on asynchronous communication, where the send messages are not directly depending on received messages.
Similar to pure sockets, first a host has to be started
val host = Host(75973, "localhost")
host.start()
Processing of received data is done with the messages
SharedFlow
, which starts consuming
received messages with the first subscriber.
host.messages.collect {
println("${it.sender}: ${it.data.bytes.decodeToString()}")
host.send(Data("Hello Client!".toByteArray()))
}
The host also provides client managing functions and events. Read the API doc for more information.
Here is a small example on a host, that dismisses all but one client.
host.events.collect { komEvent ->
if (komEvent is KomEvent.Connected && host.sessions.size > 1) {
host.disconnect(komEvent.id)
}
}
val kom = Kom()
kom.connect(75973, "localhost")
Similar to the host, the client also has a messages
flow with all the messages received from the host.
host.messages.collect {
println("Host: ${it.data.bytes.decodeToString()}")
client.send(Data("Hello Host!".toByteArray()))
}
When a kom connection has been established already, the sequentialMessaging
block can be used to perform synchronous communication.
kom.sequentialMessaging {
do {
send(Data("ping".toByteArray()))
} while(receive().data.toString() == "pong")
this.kom.disconnect()
}
host.sequentialMessaging(host.sessions.first()) {
var counter = 10
while(counter > 0 && receive().data.toString() == "ping") {
counter--
send(Data("pong".toByteArray()))
}
this.host.disconnect(ids)
}