Looks like there is no resource api for websocket....
# ktor
n
Looks like there is no resource api for websocket. So is this the correct way to do it? And should I PR to add that to ktor resource? (If so, in which module since websocket and resources are two distincts modules, and that might be the reason it is not predefined)
_webSocket_UsersApi.Id.Ws *{* params *->*
// this doesnt exist
}
_resource_UsersApi.Id.Ws *{*
_webSocket_ *{*
_handle_UsersApi.Id.Ws *{* params *->*
// route body here
}
}
}
That way doesn't work either, since it creates a route and not a selector
So I think I’ll go back to the previous string route
a
You can define the WebSockets resource builder with the following code:
Copy code
inline fun <reified T : Any> Route.webSocketRes(
    protocol: String? = null,
    noinline handler: suspend DefaultWebSocketServerSession.(T) -> Unit
) {
    resource<T> {
        val serializer = serializer<T>()
        webSocket(protocol) {
            val resources = call.application.plugin(Resources)
            val resource = resources.resourcesFormat.decodeFromParameters(serializer, call.parameters)
            handler(resource)
        }
    }
}