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

com.fireflysource.net.websocket.client.impl.AsyncWebSocketClientConnectionBuilder.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.websocket.client.impl

import com.fireflysource.common.coroutine.asVoidFuture
import com.fireflysource.net.websocket.client.WebSocketClientConnectionBuilder
import com.fireflysource.net.websocket.client.WebSocketClientConnectionManager
import com.fireflysource.net.websocket.client.WebSocketClientRequest
import com.fireflysource.net.websocket.common.WebSocketConnection
import com.fireflysource.net.websocket.common.WebSocketMessageHandler
import com.fireflysource.net.websocket.common.frame.Frame
import com.fireflysource.net.websocket.common.model.WebSocketBehavior
import com.fireflysource.net.websocket.common.model.WebSocketPolicy
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.util.concurrent.CompletableFuture

/**
 * @author Pengtao Qiu
 */
class AsyncWebSocketClientConnectionBuilder(
    private val connectionManager: WebSocketClientConnectionManager
) : WebSocketClientConnectionBuilder {

    private val request = WebSocketClientRequest()

    override fun url(url: String): WebSocketClientConnectionBuilder {
        request.url = url
        return this
    }

    override fun policy(policy: WebSocketPolicy): WebSocketClientConnectionBuilder {
        request.policy = policy
        return this
    }

    override fun extensions(extensions: List): WebSocketClientConnectionBuilder {
        request.extensions = extensions
        return this
    }

    override fun subProtocols(subProtocols: List): WebSocketClientConnectionBuilder {
        request.subProtocols = subProtocols
        return this
    }

    override fun onMessage(handler: WebSocketMessageHandler): WebSocketClientConnectionBuilder {
        request.handler = handler
        return this
    }

    override fun connect(): CompletableFuture {
        if (request.policy == null) {
            request.policy = WebSocketPolicy(WebSocketBehavior.CLIENT)
        }
        if (request.subProtocols == null) {
            request.subProtocols = listOf()
        }
        if (request.extensions == null) {
            request.extensions = listOf()
        }
        return connectionManager.connect(request)
    }

}

fun WebSocketClientConnectionBuilder.connectAsync(block: suspend CoroutineScope.(WebSocketConnection) -> Unit) {
    this.connect().thenAccept { connection -> connection.coroutineScope.launch { block(connection) } }
}

fun WebSocketClientConnectionBuilder.onClientMessageAsync(block: suspend CoroutineScope.(Frame, WebSocketConnection) -> Unit): WebSocketClientConnectionBuilder {
    this.onMessage { frame, connection -> connection.coroutineScope.launch { block(frame, connection) }.asVoidFuture() }
    return this
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy