Ktor WebSockets:kotlinx.serialization.SerializationException:未找到 class 'DefaultClientWebSocketSession' 的序列化程序
Ktor WebSockets: kotlinx.serialization.SerializationException: Serializer for class 'DefaultClientWebSocketSession' is not found
今天我正在制作一个实时应用程序,我想使用 WebSockets 来更新我的状态。
一切正常,直到出现此异常:
kotlinx.serialization.SerializationException: Serializer for class 'DefaultClientWebSocketSession' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
找啊找啊,什么都没找到。
所以我的代码看起来与此类似:
Gradle 进口:
implementation("io.ktor:ktor-client-core:1.6.2")
implementation("io.ktor:ktor-client-cio:1.6.2")
implementation("io.ktor:ktor-client-serialization:1.6.2")
implementation("io.ktor:ktor-client-websockets:1.6.2")
代码:
suspend fun main() {
val endpoint = Url("http://localhost:8080")
val client = HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
install(WebSockets)
}
client.webSocket({
url {
takeFrom(endpoint)
path("beeam")
}
}) {
//Do something
}
}
我希望有人能帮助我。
问题
问题是 endpoint.protocol
是 URLProtocol.HTTP
而不是 URLProtocol.WS
并且使用 takeFrom
你也转移了这个 属性.
解决方案
suspend fun main() {
val endpoint = Url("http://localhost:8080")
val client = HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
install(WebSockets)
}
client.webSocket({
url {
takeFrom(endpoint)
path("beeam")
protocol = URLProtocol.WS
}
}) {
//Do something
}
}
今天我正在制作一个实时应用程序,我想使用 WebSockets 来更新我的状态。
一切正常,直到出现此异常:
kotlinx.serialization.SerializationException: Serializer for class 'DefaultClientWebSocketSession' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
找啊找啊,什么都没找到。
所以我的代码看起来与此类似:
Gradle 进口:
implementation("io.ktor:ktor-client-core:1.6.2")
implementation("io.ktor:ktor-client-cio:1.6.2")
implementation("io.ktor:ktor-client-serialization:1.6.2")
implementation("io.ktor:ktor-client-websockets:1.6.2")
代码:
suspend fun main() {
val endpoint = Url("http://localhost:8080")
val client = HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
install(WebSockets)
}
client.webSocket({
url {
takeFrom(endpoint)
path("beeam")
}
}) {
//Do something
}
}
我希望有人能帮助我。
问题
问题是 endpoint.protocol
是 URLProtocol.HTTP
而不是 URLProtocol.WS
并且使用 takeFrom
你也转移了这个 属性.
解决方案
suspend fun main() {
val endpoint = Url("http://localhost:8080")
val client = HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
install(WebSockets)
}
client.webSocket({
url {
takeFrom(endpoint)
path("beeam")
protocol = URLProtocol.WS
}
}) {
//Do something
}
}