https://kotlinlang.org logo
#ktor
Title
# ktor
b

ByFloRedstone

11/30/2020, 5:45 PM
Hey how can a store a websocket session to send a message to the websocket later?
b

bbaldino

11/30/2020, 5:50 PM
There was a thread related to this the other day https://kotlinlang.slack.com/archives/C0A974TJ9/p1606123821161400
b

ByFloRedstone

11/30/2020, 5:53 PM
but i think this thread is about the client side connection i mean the server side part
b

bbaldino

11/30/2020, 5:55 PM
Ah...what I've done for that is created a channel that messages can be added to, and the server starts a coroutine which reads from that channel and sends a message whenever there is one to read
b

ByFloRedstone

11/30/2020, 5:58 PM
ok using the send method i have a problem to send a message because the method from where i want to send the message cant be a suspend fun wich is required from the send function
b

bbaldino

11/30/2020, 5:59 PM
If you want your non-suspend
send
method to wait until the message is sent, you can run it within a
runBlocking
block. If you don't want to wait, you'll want to use
launch
on some
CoroutineScope
to launch a coroutine to do the send
b

ByFloRedstone

11/30/2020, 6:00 PM
what did you mean wich "some CoroutineScope"?
b

bbaldino

11/30/2020, 6:01 PM
Well usually you launch a coroutine from some scope you've created.
GlobalScope
, for example, but I don't think it's recommended to use that except for specific use cases as tasks launched there can 'leak'.
Generally though I'd say you should read through some of the coroutine tutorials on the web to get a better feel for them if you're not familiar with
CoroutineScope
b

ByFloRedstone

11/30/2020, 6:02 PM
ah ok thanks for your help
b

bbaldino

11/30/2020, 6:03 PM
👍
b

ByFloRedstone

11/30/2020, 6:30 PM
if i run the method its only called once. If a call it twice the second message where not send to the client
b

bbaldino

11/30/2020, 6:31 PM
You'll need a loop
b

ByFloRedstone

11/30/2020, 6:45 PM
really? i have a function wich is called twice! But the message where send only once.
b

bbaldino

11/30/2020, 6:46 PM
I may not be understanding what you're describing, can you paste an example?
b

ByFloRedstone

11/30/2020, 6:48 PM
can i send it via slack with a code box or via paste server?
b

bbaldino

11/30/2020, 6:48 PM
You can paste it here, but I'd try and reduce it down if it's really big
b

ByFloRedstone

11/30/2020, 6:48 PM
Copy code
@EventListener
    fun onStart(e: CloudServicePreStartEvent) {
        sendNotification(SocketNotification("success", "Der Service ${e.cloudService.serviceId.name} startet"))
    }

    fun sendNotification(notification: SocketNotification) {
            webServer.notificationSockets.forEach {
                runBlocking {  it.send(Frame.Text(notification.toString())) }
            }
    }
that my code so the "CloudServicePreStartEvent" is called when a server starts. While the client was connected it was called 6 times, but the client only receive on message
b

bbaldino

11/30/2020, 6:50 PM
Did you verify the
runBlocking
block in
sendNotification
was called 6 times?
b

ByFloRedstone

11/30/2020, 6:52 PM
Yes i was called 6 times
b

bbaldino

11/30/2020, 6:53 PM
Ok, there might be an issue with the websocket itself, then? Maybe make sure the
send
is "finishing" each time it's called ?
b

ByFloRedstone

11/30/2020, 6:55 PM
i try to print a message before and after the send and each of then was printet in the console. So i think the send method was executed
is it possible, the error is on my javascript file?
Copy code
let socket = new WebSocket("<wss://acp.domain.com/ws>")

socket.onopen = function (event) {
    log("info", "Connected to the Notify Socket")
}

socket.onerror = function (error) {
    log("error", error.message)
}

socket.onmessage = function (m) {
    log("message", m.data)
}

function log(type, input) {
    console.log("[NOTIFY] " + type.toUpperCase() + " | " + input)
}
b

bbaldino

11/30/2020, 6:56 PM
I'd break on when you're doing the send and follow it through, maybe the websocket itself is closing or something?
b

ByFloRedstone

11/30/2020, 6:58 PM
I don't think so because when closing the websocket it will be remove from the list and on next call its in the list
b

bbaldino

11/30/2020, 7:08 PM
At this point I think you've just got to debug what's going on with the websocket
6 Views