Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
To reduce memory usage, when the web page unloads notify the server s…
Browse files Browse the repository at this point in the history
…o it can drop the ClientState, relevent to #199 and #194
  • Loading branch information
sanity committed May 23, 2021
1 parent 98b449c commit eb951f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ext {
}

group 'com.github.kwebio'
version '0.9.2'
version '0.9.3'

repositories {
mavenCentral()
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/kweb/Kweb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ class Kweb private constructor(
message.historyStateChange != null -> {

}
message.terminateConnection -> {
logger.debug { "Notified of client termination for ${message.id}, invalidating cached ClientState" }
clientState.invalidate(message.id)
}

}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/kweb/client/Client2ServerMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data class Client2ServerMessage(
val hello: Boolean? = true,
val error: ErrorMessage? = null,
val callback: C2SCallback? = null,
val terminateConnection : Boolean = false,
val historyStateChange: C2SHistoryStateChange? = null
) {

Expand Down
14 changes: 9 additions & 5 deletions src/main/resources/kweb/kweb_bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,26 @@ function sendMessage(msg) {
console.debug("Sending WebSocket message", msg);
socket.send(msg);
} else {
/*console.debug(
"Queueing WebSocket message as connection isn't established",
msg
);*/
preWSMsgQueue.push(msg);
}
}

function callbackWs(callbackId, data) {
var msg = JSON.stringify({
const msg = JSON.stringify({
id: kwebClientId,
callback: {callbackId: callbackId, data: data}
});
sendMessage(msg);
}

window.addEventListener("beforeunload", function (event) {
const msg = JSON.stringify({
id: kwebClientId,
terminateConnection: true
});
sendMessage(msg);
})

/*
* Utility functions
*/
Expand Down

0 comments on commit eb951f6

Please sign in to comment.