Hello guys, I am currently trying to connect our K...
# ktor
m
Hello guys, I am currently trying to connect our Kotlin Multiplatform Project to websockets. I would like to use ktor-websockets library to receive some updates from our backend but onfortunately when I run this code, nothing happens:
Copy code
client.webSocket(
    port = 80,
    method = HttpMethod.Get,
    host = "<https://uat-betws.sts.pl>",
    path = "/ticket?token=eyJzdWIiOiI0ZmY5Y2E1Mi02ZmEwLTRiYWYtODlhYS0wODM1NGE2MTU0YjYiLCJpYXQiOjE2MTk4MDAwNzgsImV4cCI6MTYxOTgwMzY3OH0.oIaXH-nFDpMklp4FSJWMtsM7ECSIfuNF99tTQxiEALM"
)
{
    for (message in incoming) {
        message as? Frame.Text ?: continue
        val receivedText = message.readText()
        println(receivedText)
    }
    // Consume all incoming websockets on this url
    this.incoming.consumeAsFlow().collect {
        logger.d("Received ticket status websocket of type ${it.frameType.name}")
        if (it is Frame.Text) {
            Json.decodeFromString<TicketStatusResponse>(it.readText())
        }
    }
}
Does somebody have any experience with ktor-websockets library? There is almost no documentation so maybe I am doing something wrong. Thank you Thread in #general
@Tom Wayne Can you share the code you use to send the websocket payloads also could you print any incoming frame just to check whether they're actually of type text and I am pretty sure the first loop suspends so the 2nd loop shouldn't do anything
👍 1
t
The thing is that the first block itself is not triggered. So there are no incoming Frames.
And I dont send any kind of payload to the websockets. I am just trying to receive data from it.
m
And I dont send any kind of payload to the websockets. I am just trying to receive data from it.
I meant the server side code sending it to the client
t
Unfortunately I do not have an access to it 😞
m
It was jsut another way of checking whether you actually receive text frames but you say
message as? Frame.Text ?: continue
never runs at all?
t
No. I have the breakpoints there everywhere and the block never runs
m
I wanted to try something myself but Ktor is 404ring on echo.websocket.org for me so i don't think that's going to happen today
Okay I got my code working and can receive websocket frames like this
Copy code
client.webSocket("<wss://echo.websocket.org/?encoding=text>") {
        launch {
            outgoing.send(Frame.Text("Test"))
        }

        for (frame in incoming) {
            println(frame)

            if (frame is Frame.Text) {
                println(frame.readText())
            }
        }
    }
👀 1
can you see if that works with your client
t
I will try it on my client thx.
Thank you I have found the issue. The problem was that I was using incompatible HTTP engine. Only OkHTTP, CIO and JS engine are compatible engines for Ktor-websockets
m
That should have given you an error though
t
Yeah, I got the error when I tested it on demo app. But when I tested in in project I didnt got the error in logcat for some reason
Thank you for help though. The site for testing ws is very usefull
🙂