Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Automatically reconnect the websocket connection on close #169

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions src/jsMain/kotlin/net/kyori/adventure/webui/js/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,7 @@ public fun mainLoaded() {
)

// WEBSOCKET
webSocket =
if (window.location.hostname == "localhost" ||
window.location.hostname == "127.0.0.1"
) {
WebSocket("ws://${window.location.host}$URL_API$URL_MINI_TO_HTML")
} else {
WebSocket("wss://${window.location.host}$URL_API$URL_MINI_TO_HTML")
}
webSocket.onopen = { onWebsocketReady() }
webSocket.onclose = { onWebsocketClose() }
// A closed websocket will be handled by the above, but log the error to console for debugging sake
webSocket.onerror = { err -> console.log("Websocket error: $err") }
startWebsocket()

// CORRECT HOME LINK
document.element<HTMLAnchorElement>("home-link").href = homeUrl
Expand Down Expand Up @@ -416,6 +405,18 @@ private fun readPlaceholders(): Placeholders {
return Placeholders(stringPlaceholders = stringPlaceholders)
}

private fun startWebsocket() {
webSocket = if (window.location.hostname == "localhost" || window.location.hostname == "127.0.0.1") {
WebSocket("ws://${window.location.host}$URL_API$URL_MINI_TO_HTML")
} else {
WebSocket("wss://${window.location.host}$URL_API$URL_MINI_TO_HTML")
}
webSocket.onopen = { onWebsocketReady() }
webSocket.onclose = { startWebsocket() }
// A closed websocket will be handled by the above, but log the error to console for debugging sake
webSocket.onerror = { err -> console.log("Websocket error: $err") }
}

private fun onWebsocketReady() {
// SHARING
val inputBox = document.element<HTMLTextAreaElement>("input")
Expand Down Expand Up @@ -494,16 +495,6 @@ private fun onWebsocketReady() {
}
}

private fun onWebsocketClose() {
// We no longer have a working websocket connection, so any input changes would not go through. Display a little
// warning to the user and disable the input box to bring more attention to it, since changing the input would
// have no effect at this point anyway.
val warning = document.element<HTMLTextAreaElement>("connection-lost-warning")
val inputBox = document.element<HTMLTextAreaElement>("input")
Comment on lines -501 to -502
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should remove the HTML for this too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still makes sense to have in cases where, for example the user has lost internet connection altogether, where there should be some evidence that the reconnection is being attempted

warning.hidden = false
inputBox.disabled = true
}

private fun checkClickEvents(target: EventTarget?, typesToCheck: Collection<EventType>) {
if (target is HTMLSpanElement && target.classList.contains(COMPONENT_CLASS)) {
val remainingTypesToCheck = mutableSetOf<EventType>()
Expand Down