Coder Social home page Coder Social logo

koms's Introduction

!!! This project is currently in an incubating phase. API surface, functionality and tool set might change. !!!

Koms

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.

Get started

Host setup

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)
    }
}

Client setup

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()))
}

Synchronous Koms

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)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.