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

io.javalin.http.sse.Emitter.kt Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
package io.javalin.http.sse

import jakarta.servlet.http.HttpServletResponse
import java.io.IOException
import java.io.InputStream

const val COMMENT_PREFIX = ":"
const val NEW_LINE = "\n"

class Emitter(private var response: HttpServletResponse) {

    var closed = false
        private set

    fun emit(event: String, data: InputStream, id: String?) = synchronized(this) {
        try {
            if (id != null) {
                write("id: $id$NEW_LINE")
            }
            write("event: $event$NEW_LINE")

            data.buffered().reader().useLines {
                it.forEach { line -> write("data: $line$NEW_LINE") }
            }

            write(NEW_LINE)
            response.flushBuffer()
        } catch (ignored: IOException) {
            closed = true
        }
    }

    fun emit(comment: String) =
        try {
            comment.split(NEW_LINE).forEach {
                write("$COMMENT_PREFIX $it$NEW_LINE")
            }
            response.flushBuffer()
        } catch (ignored: IOException) {
            closed = true
        }

    private fun write(value: String) =
        response.outputStream.print(value)

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy