Anti timeout ping, user list

This commit is contained in:
Maxwell Jeffress 2024-10-28 20:37:20 +11:00
parent 97a26f7615
commit e06f3891c8
2 changed files with 74 additions and 13 deletions

View File

@ -24,6 +24,7 @@ tasks.withType<Jar> {
dependencies { dependencies {
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("io.javalin:javalin:6.3.0") implementation("io.javalin:javalin:6.3.0")
implementation("org.slf4j:slf4j-simple:2.0.16") implementation("org.slf4j:slf4j-simple:2.0.16")
} }

View File

@ -5,6 +5,8 @@ import io.javalin.websocket.WsContext
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.UUID import java.util.UUID
import kotlin.concurrent.fixedRateTimer
import java.io.File import java.io.File
import java.io.BufferedReader import java.io.BufferedReader
@ -17,12 +19,50 @@ fun md5(input:String): String {
} }
object WsSessionManager { object WsSessionManager {
var peopleOnline = mutableListOf("")
var sessionsList = mutableListOf("")
private val sessions = ConcurrentHashMap<WsContext, String>() private val sessions = ConcurrentHashMap<WsContext, String>()
private val sessionIds = ConcurrentHashMap<String, WsContext>() private val sessionIds = ConcurrentHashMap<String, WsContext>()
init {
fixedRateTimer("websocket-ping", period = 5000) {
sendPing()
}
}
private fun sendPing() {
val deadSessions = mutableListOf<WsContext>()
sessions.keys.forEach { ctx ->
try {
if (ctx.session.isOpen) {
ctx.send("ping")
} else {
deadSessions.add(ctx)
}
} catch (e: Exception) {
println("Error sending ping: ${e.message}")
deadSessions.add(ctx)
}
}
// Clean up any dead sessions
deadSessions.forEach { removeSession(it) }
}
fun broadcastOnlineUsers() {
broadcast("!users:{${peopleOnline.joinToString(",")}}")
}
fun handleUserLogin(username: String) {
peopleOnline += username
broadcastOnlineUsers()
}
fun addSession(ctx: WsContext) { fun addSession(ctx: WsContext) {
try { try {
val sessionId = UUID.randomUUID().toString() val sessionId = UUID.randomUUID().toString()
sessionsList += sessionId
sessions[ctx] = sessionId sessions[ctx] = sessionId
sessionIds[sessionId] = ctx sessionIds[sessionId] = ctx
} catch (e: Exception) { } catch (e: Exception) {
@ -34,8 +74,11 @@ object WsSessionManager {
try { try {
val sessionId = sessions[ctx] val sessionId = sessions[ctx]
if (sessionId != null) { if (sessionId != null) {
peopleOnline.removeAt(sessionsList.indexOf(sessionId))
sessionsList.removeAt(sessionsList.indexOf(sessionId))
sessions.remove(ctx) sessions.remove(ctx)
sessionIds.remove(sessionId) sessionIds.remove(sessionId)
broadcastOnlineUsers()
} }
} catch (e: Exception) { } catch (e: Exception) {
println("Error removing session: ${e.message}") println("Error removing session: ${e.message}")
@ -101,6 +144,7 @@ fun handleSentMessage(inputData: String): String {
var message = "" var message = ""
var dataType = "" var dataType = ""
var command = "" var command = ""
var commandArg = ""
var isParsingData = 0 var isParsingData = 0
for (char in inputData) { for (char in inputData) {
val character = char val character = char
@ -119,6 +163,8 @@ fun handleSentMessage(inputData: String): String {
message += character message += character
} else if (dataType == "command") { } else if (dataType == "command") {
command += character command += character
} else if (dataType == "commandArg") {
commandArg += character
} }
} }
} else { } else {
@ -177,9 +223,17 @@ fun handleSentMessage(inputData: String): String {
chatHistory.appendText("$username: $message ${System.lineSeparator()}") chatHistory.appendText("$username: $message ${System.lineSeparator()}")
message = "" message = ""
return("Success") return("Success")
} else if (command != "") {
if (command == "sync") {
return(chatHistoryView.readText())
} else if (command == "login") {
WsSessionManager.handleUserLogin(commandArg)
return("Login successful")
}
} else { } else {
return("No data provided") return("No data provided")
} }
return("System: Welcome to Chookpen, $username!")
} }
fun syncMessages(inputData: String): String { fun syncMessages(inputData: String): String {
@ -391,7 +445,8 @@ fun authKey(inputData: String): String {
} }
fun main(args: Array<String>) { fun main(args: Array<String>) {
var users = listOf("") WsSessionManager.peopleOnline.removeAt(0)
WsSessionManager.sessionsList.removeAt(0)
val app = Javalin.create() val app = Javalin.create()
.get("/") { ctx -> ctx.result("dingus") } .get("/") { ctx -> ctx.result("dingus") }
.get("/api/send/{content}") { ctx -> .get("/api/send/{content}") { ctx ->
@ -414,19 +469,24 @@ fun main(args: Array<String>) {
WsSessionManager.removeSession(ctx) WsSessionManager.removeSession(ctx)
} }
ws.onMessage { ctx -> ws.onMessage { ctx ->
println(ctx.message()) when (ctx.message()) {
val successState = handleSentMessage(ctx.message()) "pong" -> {}
if (successState != "Success") { else -> {
try { println(ctx.message())
ctx.send(successState) val successState = handleSentMessage(ctx.message())
} catch (e: Exception) { if (successState != "Success") {
println("Error sending error message: ${e.message}") try {
ctx.send(successState)
} catch (e: Exception) {
println("Error sending error message: ${e.message}")
}
} else {
val messageContent = extractMessageContent(ctx.message())
WsSessionManager.broadcast(messageContent)
} }
} else {
val messageContent = extractMessageContent(ctx.message())
WsSessionManager.broadcast(messageContent)
} }
} }
}
} }
.start(7070) .start(7070)