Kotlin CUBA平台将消息从后端推送到UI
我想知道是否可以从后端(例如从外部系统接收信息的正在运行的任务)向UI发送消息。在我的例子中,它需要是一个特定的会话(没有广播),并且只在特定的屏幕上 B计划将经常轮询后端,但我希望得到更“实时”的东西 我试图解决这样的问题,但我一直遇到NotSerializableExceptionKotlin CUBA平台将消息从后端推送到UI,kotlin,server-push,cuba-platform,Kotlin,Server Push,Cuba Platform,我想知道是否可以从后端(例如从外部系统接收信息的正在运行的任务)向UI发送消息。在我的例子中,它需要是一个特定的会话(没有广播),并且只在特定的屏幕上 B计划将经常轮询后端,但我希望得到更“实时”的东西 我试图解决这样的问题,但我一直遇到NotSerializableException @Push class StorageAccess : Screen(), MessageListener { @Inject private lateinit var stationWSServ
@Push
class StorageAccess : Screen(), MessageListener {
@Inject
private lateinit var stationWSService: StationWebSocketService
@Inject
private lateinit var notifications: Notifications
@Subscribe
private fun onInit(event: InitEvent) {
}
@Subscribe("stationPicker")
private fun onStationPickerValueChange(event: HasValue.ValueChangeEvent<StorageUnit>) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
val prevValue = event.prevValue
if (prevValue != null) {
stationWSService.remove(current.userSession.id)
}
val value = event.value ?: return
stationWSService.listen(current.userSession.id, value, this)
}
override fun messageReceived(message: String) {
val current = AppUI.getCurrent()
current.access {
notifications.create().withCaption(message).show()
}
}
@Subscribe
private fun onAfterDetach(event: AfterDetachEvent) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
stationWSService.remove(current.userSession.id)
}
}
--我的后端服务的listen方法
private val listeners: MutableMap<String, MutableMap<UUID, MessageListener>> = HashMap()
override fun listen(id: UUID, storageUnit: StorageUnit, callback: MessageListener) {
val unitStationIP: String = storageUnit.unitStationIP ?: return
if (!listeners.containsKey(unitStationIP))
listeners[unitStationIP] = HashMap()
listeners[unitStationIP]?.set(id, callback)
}
private val侦听器:MutableMap=HashMap()
覆盖有趣的侦听(id:UUID,storageUnit:storageUnit,回调:MessageListener){
val unitStationIP:String=storageUnit.unitStationIP?:返回
if(!listeners.containsKey(unitStationIP))
监听器[unitStationIP]=HashMap()
侦听器[unitStationIP]?.set(id,回调)
}
我得到的异常是NotSerializableException:com.haulmont.cuba.web.sys.WebNotifications
,在将侦听器添加到后端时发生:stationWSService.listen(current.userSession.id,value,this)
据我所知,这是UI将信息发送到后端的地方,同时也是StorageAccess类的整个状态,包括其所有成员
有一个优雅的解决方案吗
关于有一个插件正好解决了这个问题:太棒了,看起来很有希望。我试过了,成功了。有没有什么优雅的方式可以发送给某些用户/会话?否则,我仍然可以处理这个问题并找到解决方法。也许您可以在事件和侦听器中发送用户名或会话id,将传入的信息与当前用户会话进行比较,只是忽略不同用户的事件。
private val listeners: MutableMap<String, MutableMap<UUID, MessageListener>> = HashMap()
override fun listen(id: UUID, storageUnit: StorageUnit, callback: MessageListener) {
val unitStationIP: String = storageUnit.unitStationIP ?: return
if (!listeners.containsKey(unitStationIP))
listeners[unitStationIP] = HashMap()
listeners[unitStationIP]?.set(id, callback)
}