Fix web map blanking and websocket dropping behind a reverse proxy

- web-map: main() was bound to the whole site state, so every state
  update (e.g. each player position update over the websocket)
  re-rendered the page and re-attached the Leaflet map to a new
  element, blanking the tiles. Only re-render when the view, server
  list, or map instance changes.
- web-map: reconnect the websocket when it closes, instead of silently
  stopping live updates.
- events-server-web: send websocket pings every 30s, so idle
  connections aren't closed by reverse proxies (nginx defaults to a
  60s read timeout).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lazzexus 2026-10-12 18:09:03 +02:00
parent e52ea9d9cf
commit d023ec61d9
3 changed files with 26 additions and 7 deletions

View file

@ -13,6 +13,9 @@ fun Application.webmapWebsocketServer(
wsServer: WebmapWebsocketServer,
) {
install(WebSockets) {
// keep idle connections alive through reverse proxies (e.g. nginx closes after 60s by default)
pingPeriodMillis = 30_000
extensions {
install(WebSocketDeflateExtension) {
// Compression level to use for java.util.zip.Deflater

View file

@ -26,6 +26,7 @@ import io.kvision.redux.createReduxStore
import io.kvision.require
import io.kvision.startApplication
import io.kvision.state.bind
import io.kvision.state.sub
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -78,7 +79,13 @@ object App : Application() {
headerNav(state)
}
main().bind(siteStateStore) { state ->
// Only re-render when the page itself changes. Re-rendering on every state update (e.g. each
// player position update) re-attaches the Leaflet map to a new element, which blanks the map.
val pageState = siteStateStore.sub { state ->
Triple(state.view, state.serverIds, state.factorioGameState?.map)
}
main().bind(pageState) { _ ->
val state = siteStateStore.getState()
div(className = "container-fluid") {
when (state.view) {
SiteView.HOME -> homePage(state)

View file

@ -18,6 +18,7 @@ import dev.adamko.kafkatorio.webmap.config.ApplicationProperties
import dev.adamko.kafkatorio.webmap.config.jsonMapper
import dev.adamko.kafkatorio.webmap.rootJob
import kotlin.coroutines.CoroutineContext
import kotlinx.browser.window
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -36,17 +37,25 @@ object WebsocketService : CoroutineScope {
override val coroutineContext: CoroutineContext =
Dispatchers.Default + CoroutineName("WebsocketService") + Job(rootJob)
private val ws = WebSocket(ApplicationProperties.websocketServerUrl)
private const val RECONNECT_DELAY_MS = 5_000
private var ws: WebSocket = connect()
private val packetsMutableFlow = MutableSharedFlow<EventServerPacket>()
val packetsFlow: SharedFlow<EventServerPacket>
get() = packetsMutableFlow.asSharedFlow()
init {
ws.onmessage = ::handleMessageEvent
ws.onerror = ::handleError
println("[WebsocketService] init ${ws.url}, ${ws.protocol}")
/** Open a websocket, and automatically reconnect if it closes (e.g. server restart, proxy timeout). */
private fun connect(): WebSocket {
val socket = WebSocket(ApplicationProperties.websocketServerUrl)
socket.onmessage = ::handleMessageEvent
socket.onerror = ::handleError
socket.onclose = {
println("[WebsocketService] closed, reconnecting in ${RECONNECT_DELAY_MS}ms")
window.setTimeout({ ws = connect() }, RECONNECT_DELAY_MS)
}
println("[WebsocketService] connecting ${socket.url}")
return socket
}
private fun handleMessageEvent(msg: MessageEvent) {