Skip to content

joffrey-bion/socketio-kotlin

Repository files navigation

socketio-kotlin

Maven central version Github Build GitHub license

A Kotlin parser for Socket.IO / Engine.IO packet decoding

Setup

Add the dependency:

dependencies {
    implementation("org.hildan.socketio:socketio-kotlin:$version")
}

Usage

val encodedTextPacket: String = TODO("get an encoded packet, for instance a web socket frame body")

val engineIOPacket = EngineIO.decodeSocketIO(encodedTextPacket)

when (engineIOPacket) {
    is EngineIOPacket.Open,
    is EngineIOPacket.Close,
    is EngineIOPacket.Upgrade,
    is EngineIOPacket.Noop,
    is EngineIOPacket.Ping,
    is EngineIOPacket.Pong -> TODO("process transport Engine.IO packet")
    is EngineIOPacket.Message -> when (val socketIOPacket = engineIoPacket.payload) {
        is SocketIOPacket.Connect,
        is SocketIOPacket.ConnectError,
        is SocketIOPacket.Disconnect -> TODO("process transport Socket.IO packet")
        is SocketIOPacket.Event,
        is SocketIOPacket.Ack -> {
            val namespace = socketIOPacket.namespace
            val eventType = socketIOPacket.payload[0].jsonPrimitive.content
            val eventPayload = socketIOPacket.payload[1].jsonObject
            println("Received Socket.IO event $eventType at $namespace: $eventPayload")
        }
    }
}