All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonJvm.dev.icerock.moko.socket.Socket.kt Maven / Gradle / Ivy

/*
 * Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
 */

package dev.icerock.moko.socket

import io.socket.client.IO
import io.socket.engineio.client.transports.Polling
import io.socket.engineio.client.transports.WebSocket
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import org.json.JSONArray
import org.json.JSONObject
import io.socket.client.Socket as SocketIo

actual class Socket actual constructor(
    endpoint: String,
    config: SocketOptions?,
    build: SocketBuilder.() -> Unit
) {
    private val socketIo: SocketIo

    init {

        socketIo = IO.socket(endpoint, IO.Options().apply {
            transports = config?.transport?.let {
                when (it) {
                    SocketOptions.Transport.DEFAULT -> return@let null
                    SocketOptions.Transport.WEBSOCKET -> return@let arrayOf(WebSocket.NAME)
                    SocketOptions.Transport.POLLING -> return@let arrayOf(Polling.NAME)
                }
            }
            query = config?.queryParams?.run {
                if (size == 0) return@run null

                val params: List = map { (key, value) -> "$key=$value" }
                params.joinToString("&")
            }
        })

        object : SocketBuilder {
            override fun on(event: String, action: Socket.(message: String) -> Unit) {
                socketIo.on(event) {
                    [email protected](rawDataToString(it))
                }
            }

            override fun  on(socketEvent: SocketEvent, action: Socket.(data: T) -> Unit) {
                socketEvent.socketIoEvents.forEach { event ->
                    socketIo.on(event) { data ->
                        [email protected](socketEvent.mapper(data))
                    }
                }
            }
        }.build()
    }

    actual fun emit(event: String, data: JsonObject) {
        socketIo.emit(event, JSONObject(data.toString()))
    }

    actual fun emit(event: String, data: JsonArray) {
        socketIo.emit(event, JSONArray(data.toString()))
    }

    actual fun emit(event: String, data: String) {
        socketIo.emit(event, data)
    }

    actual fun connect() {
        socketIo.connect()
    }

    actual fun disconnect() {
        socketIo.disconnect()
    }

    actual fun isConnected(): Boolean {
        return socketIo.connected()
    }

    private companion object {
        fun rawDataToString(data: Array): String {
            return data.last().toString()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy