Skip to content

Commit 49fee66

Browse files
committed
refactor: use the correlation provided by the client for the socket connection instead of generating a random UUID manually
1 parent 64e53a2 commit 49fee66

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

src/main/kotlin/com/sakethh/linkora/WebSocket.kt

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,51 @@ package com.sakethh.linkora
22

33
import com.sakethh.linkora.WebSocketManager.closeWriteChannel
44
import com.sakethh.linkora.WebSocketManager.initializeWriteChannel
5+
import com.sakethh.linkora.domain.dto.Correlation
56
import io.ktor.server.application.*
67
import io.ktor.server.auth.*
78
import io.ktor.server.routing.*
89
import io.ktor.server.websocket.*
9-
import java.util.*
10+
import io.ktor.websocket.*
11+
import kotlinx.serialization.json.Json
1012

1113
fun Application.eventsWebSocket() {
1214
routing {
1315
authenticate(Security.BEARER.name) {
14-
webSocket("/events") {
15-
val sessionId = UUID.randomUUID().toString()
16-
initializeWriteChannel(sessionId)
16+
webSocket(path = "/events") {
17+
val correlationParam = call.parameters["correlation"]
18+
if (correlationParam == null) {
19+
this.close(
20+
CloseReason(
21+
message = "Expected `correlation` as an encoded JSON string via parameter, but it was not provided.",
22+
code = CloseReason.Codes.CANNOT_ACCEPT
23+
)
24+
)
25+
return@webSocket
26+
}
27+
val correlation = try {
28+
Json.decodeFromString<Correlation>(correlationParam)
29+
} catch (e: Exception) {
30+
e.printStackTrace()
31+
this.close(
32+
CloseReason(
33+
message = "The schema of the provided JSON does not match the expected format.",
34+
code = CloseReason.Codes.CANNOT_ACCEPT
35+
)
36+
)
37+
return@webSocket
38+
}
39+
val correlationId = correlation.id
40+
initializeWriteChannel(correlationId)
41+
println("Established the `events` socket connection with \"${correlation.clientName}\".")
1742
try {
1843
for (frame in incoming) {
1944
}
2045
} catch (e: Exception) {
21-
println("WebSocket error: ${e.message}")
46+
println("WebSocket error for \"${correlation.clientName}\": ${e.message}")
2247
} finally {
23-
closeWriteChannel(sessionId)
24-
println("WebSocket closed.")
48+
closeWriteChannel(correlationId)
49+
println("WebSocket closed for \"${correlation.clientName}\".")
2550
}
2651
}
2752
}

src/main/kotlin/com/sakethh/linkora/WebSocketManager.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ object WebSocketManager {
1313
prettyPrint = true
1414
}
1515

16-
fun DefaultWebSocketServerSession.initializeWriteChannel(sessionId: String) {
17-
writeChannels[sessionId] = this
16+
fun DefaultWebSocketServerSession.initializeWriteChannel(correlationId: String) {
17+
writeChannels[correlationId] = this
1818
}
1919

2020
suspend fun sendEvent(notification: WebSocketEvent) {
21-
writeChannels.forEach { (sessionId, session) ->
21+
writeChannels.forEach { (correlationId, session) ->
2222
try {
2323
session.send(Frame.Text(json.encodeToString(notification)))
24-
println("Sent event to the client with sessionId: $sessionId")
24+
println("Sent event to the client : $correlationId")
2525
} catch (e: Exception) {
26-
println("removing the client $sessionId due to ${e.message}")
27-
closeWriteChannel(sessionId)
26+
println("removing the client $correlationId due to ${e.message}")
27+
closeWriteChannel(correlationId)
2828
e.printStackTrace()
2929
}
3030
}
3131
}
3232

33-
fun closeWriteChannel(sessionId: String) {
34-
writeChannels.remove(sessionId)
33+
fun closeWriteChannel(correlationId: String) {
34+
writeChannels.remove(correlationId)
3535
}
3636
}

0 commit comments

Comments
 (0)