首页 > 解决方案 > CUBA 平台将消息从后端推送到 UI

问题描述

我想知道是否可以将消息从后端(例如从外部系统接收信息的正在运行的任务)发送到 UI。在我的情况下,它需要是一个特定的会话(没有广播)并且只在一个特定的屏幕上

计划 B 会经常轮询后端,但我希望得到更“实时”的东西

我试图解决这样的问题,但我不断收到 NotSerializableException。

@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)
    }

}

-- 回调接口

interface MessageListener : Serializable {
    fun messageReceived(message: String);
}

-- 我的后端服务的监听方法

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)
}

我得到的异常是NotSerializableException: com.haulmont.cuba.web.sys.WebNotifications在将侦听器添加到后端期间发生的:stationWSService.listen(current.userSession.id, value, this)

据我了解,这是 UI 将信息发送到后端的地方 - 以及 StorageAccess 类的整个状态,包括其所有成员。

有一个优雅的解决方案吗?

问候

标签: kotlinserver-pushcuba-platform

解决方案


有一个插件可以解决这个问题:https ://github.com/cuba-platform/global-events-addon


推荐阅读