bbaldino
05/03/2022, 10:44 PMwebSocket
block? I’m trying to do something like:
webSocket("/ws") {
val webSocketTransport = WebSocketTransport(this)
eventHandler.newWebSocketConnected(webSocketTransport)
}
where the WebSocketTransport
launches a coroutine to read the incoming messages:
scope.launch {
for (msg in webSocketSession.incoming) {
...
}
}
but when I try this, the websocket disconnects immediately, because it looks like it’s implemented in a way to close as soon as the passed handler
returns. Is there any good pattern move that logic out? I did it one way where I added another channel, and the webSocket handler read messages and wrote them to that channel, and WebSocketTransport read from that channel, but was hoping to avoid having to create the intermediary.ephemient
05/03/2022, 10:49 PMscope
is the current scope or a child then it will all work outbbaldino
05/03/2022, 10:52 PMWebSocketTransport
?scope.launch
ephemient
05/03/2022, 10:53 PMbbaldino
05/03/2022, 10:56 PMwebSocket("/ws") {
coroutineScope {
val webSocketTransport = WebSocketTransport(this@webSocket, this@coroutineScope)
handler.newWebSocketConnection(sessionId, webSocketTransport)
}
}
which seems to be working right…does that look reasonable?scope.launch
is done in the WebSocketTransport.init
, so it kicks that off before coroutineScope
closes, which I’m assuming is required to keep the parent webSocket scope “alive”ephemient
05/03/2022, 10:59 PMthis@webSocket
should already implement CoroutineScope
bbaldino
05/03/2022, 11:00 PMwebSocketSession.launch
doesn’t seem to keep it alive.ephemient
05/03/2022, 11:01 PMcoroutineScope {}
worksbbaldino
05/03/2022, 11:03 PMlaunch
alone doesn’t create a child scope? (does it?)ephemient
05/03/2022, 11:04 PMbbaldino
05/03/2022, 11:04 PMhandler
finishes, it closes itRustam Siniukov
05/04/2022, 9:30 AMclient.webSocketSession(request)
if you need manual control over closingbbaldino
05/04/2022, 3:32 PMrouting
?Rustam Siniukov
05/05/2022, 6:53 AMAleksei Tirman [JB]
05/08/2022, 3:32 PM