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

commonMain.com.ditchoom.websocket.WebSocketClientToServerBinaryFrameTransformer.kt Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package com.ditchoom.websocket

import com.ditchoom.buffer.PlatformBuffer
import com.ditchoom.buffer.allocateNewBuffer
import com.ditchoom.data.DataTransformer
import kotlin.random.Random

/**
 * Transforms a buffer into a websocket wrapped buffer which can then be sent directly on the socket
 */
@ExperimentalUnsignedTypes
object WebSocketClientToServerBinaryFrameTransformer : DataTransformer {

    override suspend fun transform(input: PlatformBuffer): PlatformBuffer {
        val applyFin = true
        val bytes = Random.nextBytes(4)
        val frame = WebSocketFrame(applyFin, Opcode.Binary, MaskingKey.FourByteMaskingKey(bytes), input)
        val websocketEncodedBuffer = allocateNewBuffer(frame.size().toUInt())
        frame.serialize(websocketEncodedBuffer)
        return websocketEncodedBuffer
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy