feat(core, network): lokale Chat-Kommunikation und WebSocket-Server hinzugefügt
Signed-off-by: Stefan Mogeritsch <stefan.mo.co@gmail.com>
This commit is contained in:
+2
-1
@@ -1,5 +1,6 @@
|
||||
package at.mocode.frontend.core.network
|
||||
|
||||
import at.mocode.frontend.core.network.chat.chatModule
|
||||
import at.mocode.frontend.core.network.discovery.discoveryModule
|
||||
import at.mocode.frontend.core.network.sync.syncModule
|
||||
import io.ktor.client.*
|
||||
@@ -26,7 +27,7 @@ interface TokenProvider {
|
||||
* - "apiClient": Konfigurierter Client für das API-Gateway (Auth-Header, Retry, Timeout)
|
||||
*/
|
||||
val networkModule: Module = module {
|
||||
includes(discoveryModule, syncModule)
|
||||
includes(discoveryModule, syncModule, chatModule)
|
||||
|
||||
single<ConnectivityTracker> { ConnectivityTracker() }
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package at.mocode.frontend.core.network.chat
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* Einfaches Chat-Message Modell für lokale Host/Client-Kommunikation.
|
||||
*/
|
||||
@Serializable
|
||||
data class ChatMessage(
|
||||
val sender: String,
|
||||
val message: String,
|
||||
val timestamp: Long
|
||||
)
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package at.mocode.frontend.core.network.chat
|
||||
|
||||
import org.koin.core.module.Module
|
||||
|
||||
/**
|
||||
* Erwartetes Koin-Modul für Chat/WS-Server.
|
||||
*/
|
||||
expect val chatModule: Module
|
||||
+3
-1
@@ -9,6 +9,8 @@ data class DiscoveredService(
|
||||
val name: String,
|
||||
val host: String,
|
||||
val port: Int,
|
||||
/** Optional: expliziter WebSocket-Port, falls vom Haupt-Port abweichend. */
|
||||
val websocketPort: Int? = null,
|
||||
val metadata: Map<String, String> = emptyMap()
|
||||
)
|
||||
|
||||
@@ -36,7 +38,7 @@ interface NetworkDiscoveryService {
|
||||
|
||||
/**
|
||||
* Registriert den eigenen Dienst, damit andere Instanzen ihn finden können.
|
||||
* @param port Der Port, auf dem der lokale WebSocket-Server lauscht.
|
||||
* @param port Der Haupt-Port des Dienstes (z. B. HTTP/API). Der WebSocket-Port wird zusätzlich als Metadatum veröffentlicht.
|
||||
* @param preferredIp Optional eine IP-Adresse, an die der Discovery-Dienst gebunden werden soll.
|
||||
* @param deviceName Der Name des Geräts, das im Netzwerk angezeigt werden soll.
|
||||
*/
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package at.mocode.frontend.core.network.chat
|
||||
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.dsl.module
|
||||
|
||||
actual val chatModule: Module = module {
|
||||
// Ktor WebSocket Server (lokaler Host)
|
||||
single { KtorWebSocketServerService() }
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package at.mocode.frontend.core.network.chat
|
||||
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.netty.*
|
||||
import io.ktor.server.plugins.contentnegotiation.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.websocket.*
|
||||
import io.ktor.websocket.*
|
||||
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Einfacher Ktor WebSocket Server für lokale Chat-Kommunikation.
|
||||
*/
|
||||
class KtorWebSocketServerService(
|
||||
private val port: Int = DEFAULT_PORT
|
||||
) {
|
||||
|
||||
private var server: EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration>? = null
|
||||
|
||||
private val connections = mutableSetOf<DefaultWebSocketServerSession>()
|
||||
private val lock = Mutex()
|
||||
|
||||
fun start() {
|
||||
if (server != null) return
|
||||
|
||||
val engine = embeddedServer(Netty, port = port, host = "0.0.0.0") {
|
||||
install(WebSockets)
|
||||
install(ContentNegotiation) {
|
||||
json(Json)
|
||||
}
|
||||
|
||||
routing {
|
||||
webSocket("/chat") {
|
||||
// Verbindung merken
|
||||
lock.withLock { connections.add(this) }
|
||||
try {
|
||||
for (frame in incoming) {
|
||||
when (frame) {
|
||||
is Frame.Text -> {
|
||||
val text = frame.readText()
|
||||
// JSON -> ChatMessage
|
||||
val msg = try {
|
||||
Json.decodeFromString(ChatMessage.serializer(), text)
|
||||
} catch (e: Exception) {
|
||||
application.log.warn("[WS] Ungültige Nachricht: ${e.message}")
|
||||
continue
|
||||
}
|
||||
// Broadcast an alle Clients
|
||||
broadcast(msg)
|
||||
}
|
||||
|
||||
is Frame.Binary -> {
|
||||
// Ignorieren oder in Zukunft unterstützen
|
||||
}
|
||||
|
||||
is Frame.Ping, is Frame.Pong, is Frame.Close -> {
|
||||
// nichts
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_: ClosedReceiveChannelException) {
|
||||
// Verbindung wurde geschlossen
|
||||
} catch (e: Exception) {
|
||||
application.log.error("[WS] Fehler in Session: ${e.message}", e)
|
||||
} finally {
|
||||
lock.withLock { connections.remove(this) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
engine.start(wait = false)
|
||||
server = engine
|
||||
println("[WS] Ktor WebSocket Server gestartet auf Port $port (Pfad: /chat)")
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
server?.stop(gracePeriodMillis = 1000, timeoutMillis = 3000)
|
||||
server = null
|
||||
println("[WS] Ktor WebSocket Server gestoppt")
|
||||
}
|
||||
|
||||
suspend fun broadcast(message: ChatMessage) {
|
||||
val json = Json.encodeToString(ChatMessage.serializer(), message)
|
||||
val snapshot: List<DefaultWebSocketServerSession> = lock.withLock { connections.toList() }
|
||||
snapshot.forEach { session ->
|
||||
try {
|
||||
session.send(Frame.Text(json))
|
||||
} catch (e: Exception) {
|
||||
// Fehler beim Senden ignorieren; Verbindung wird beim nächsten Empfang entfernt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getPort(): Int = port
|
||||
|
||||
companion object {
|
||||
const val DEFAULT_PORT: Int = 8081
|
||||
}
|
||||
}
|
||||
+7
-2
@@ -51,11 +51,14 @@ class JmDnsDiscoveryService : NetworkDiscoveryService {
|
||||
|
||||
override fun serviceResolved(event: ServiceEvent) {
|
||||
val info = event.info
|
||||
val md = info.propertyNames.asSequence().associateWith { info.getPropertyString(it) }
|
||||
val wsPort = md["websocketPort"]?.toIntOrNull()
|
||||
val service = DiscoveredService(
|
||||
name = event.name,
|
||||
host = info.inetAddresses.firstOrNull()?.hostAddress ?: "unknown",
|
||||
port = info.port,
|
||||
metadata = info.propertyNames.asSequence().associateWith { info.getPropertyString(it) }
|
||||
websocketPort = wsPort,
|
||||
metadata = md
|
||||
)
|
||||
discoveredServicesMap[event.name] = service
|
||||
_discoveredServices.value = discoveredServicesMap.values.toList()
|
||||
@@ -103,7 +106,9 @@ class JmDnsDiscoveryService : NetworkDiscoveryService {
|
||||
mapOf(
|
||||
"version" to "1.0.0",
|
||||
"type" to "master",
|
||||
"nodeId" to name
|
||||
"nodeId" to name,
|
||||
// Der Ktor WebSocket-Server lauscht (derzeit) auf demselben Port; kann abweichen
|
||||
"websocketPort" to port.toString()
|
||||
)
|
||||
)
|
||||
try {
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package at.mocode.frontend.core.network.chat
|
||||
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.dsl.module
|
||||
|
||||
// Auf WASM/JS gibt es keinen lokalen Ktor-Server; bereiten ein leeres Modul vor.
|
||||
actual val chatModule: Module = module { }
|
||||
Reference in New Issue
Block a user