Adding an encoder to a websocket server

i am writing a websocket and want to add an encoder to my websocket-server. this is my encoder

class PersonEncoder : Encoder.Text<Person> {
    override fun init(ec: EndpointConfig) {}

    override fun destroy() {}

    @Throws(EncodeException::class)
    override fun encode(msgA: Person): String {
        val gson = Gson()
        return gson.toJson(msgA)
    }
}

and here i try to add the encoder to the server:

@ServerEndpoint("/echo", configurator = ServletAwareConfig::class, encoders = { arrayOf(PersonEncoder::class)})
class SocketServer {

but i get following error:

Kotlin: Type mismatch: inferred type is () -> Array<KClass<PersonEncoder>> but Array<KClass<out Encoder!>> was expected

i also tried more close to java code.

@ServerEndpoint("/echo", configurator = ServletAwareConfig::class, encoders = { PersonEncoder::class })

but then get the error

Kotlin: Type mismatch: inferred type is () -> KClass<PersonEncoder> but Array<KClass<out Encoder!>> was expected

how can i fix this?

1 Like

The curly braces that you’re using in the annotation parameter create a lambda. Remove them, and leave only arrayOf() in.

1 Like