kotlin - Ktor reflection to create custom WebSocket session doesn't update constructor parameters
问题描述
I'm working on a web app written with a Kotlin backend and React frontend, that uses Ktor to set up a web server. I'm currently trying to add functionality that requires a WebSocket connection, and to maintain state across the WebSocket session, using the custom session model from the Ktor example chat app here: https://github.com/ktorio/ktor-samples/tree/master/app/chat/src.
However, I'm getting an issue with the reflection Ktor is doing to create these sessions.
The custom session is defined as follows:
data class THavalonUserSession(val id: String,
var name: String, var socket: DefaultWebSocketSession?)
And is initialized in the following block:
intercept(ApplicationCallPipeline.Features) {
if (call.sessions.get<THavalonUserSession>() == null) {
call.sessions.set(THavalonUserSession(generateNonce(), "", null))
}
}
When I run the application on Chrome, either with built React or in React development mode, I get the following server side error:
java.lang.IllegalArgumentException: Couldn't instantiate type class main.THavalonUserSession for parameters [id, name]
When I run it on Firefox, it works, until I change the socket parameter name from socket to mySocket, at which point it gives the error:
java.lang.IllegalArgumentException: Couldn't instantiate type class main.THavalonUserSession for parameters [id, name, socket]
The only thing I can think of is that it's somehow caching class names in the browser, but that makes no sense to me.
解决方案
Turns out Ktor stores reflection data in local storage. Why? I have no idea. Clear browser data to solve this issue.
推荐阅读
- spring - Spring 无法将属性绑定到“DataSourceProperties”
- android - 首次加载长消息列表时如何到达回收视图的底部
- c++ - 具有类型模板参数默认值的非类型模板参数
- ios - 返回 nil 的自定义 tableview 单元格 xib 出口
- angular - 当使用角度更改标题组件数据时,如何将数据加载到选定的仪表板组件中?
- pyspark - 在 Spark 版本 3.0.0 中加载和应用 MultilayerPerceptronClassifier 时的问题/错误
- tensorflow - sess.run() 将值输入占位符张量的问题
- for-loop - Bash - 用于循环 curl 没有输出
- nginx - SignalR 完成 CORS 协商,但与 nginx 代理的集线器连接失败。直接连接没问题,即使使用 CORS
- go - 获取主要版本模块依赖项时出错