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

Kirill Prybylsky

05/10/2020, 2:15 PM
Hello everyone, I am trying to implement websocket feature with ktor, but I only get 404 for the http://localhost:8080/ request in browser. Could you, please, tell me what can be wrong? Here is my websocket implementation:
Copy code
webSocket("/") {
    while (true) {
        delay(3000)
        outgoing.send(Frame.Text("hi"))
    }
}
r

rharter

05/10/2020, 3:32 PM
Hitting that from the browser isn't making a websocket request, it's an http request. They are fundamentally different. You'd have to use a web socket client to make the request there, AFAIK
k

Kirill Prybylsky

05/10/2020, 3:33 PM
I also tried to use chrome extentions to make that call with ws:// scheme. But it still not found
r

rharter

05/10/2020, 3:34 PM
What's your main config look like? Do you have the right port?
k

Kirill Prybylsky

05/10/2020, 3:35 PM
Copy code
ktor {
    application {
        modules = [com.pinkydev.server.MainAppKt.main]
    }
}
This is the config file
r

rharter

05/10/2020, 3:36 PM
But what port is that listening on? If you make an http get handler at / does that work on port 8080?
k

Kirill Prybylsky

05/10/2020, 3:37 PM
From the console output it runs on localhost:8080/
👍 1
r

rharter

05/10/2020, 3:38 PM
Also, I notice you aren't using the incoming receive channel at all in your handler. Perhaps that's the problem? I think this code might just get stuck in q loop since it's not using
for (frame in incoming)
k

Kirill Prybylsky

05/10/2020, 3:39 PM
I thought it isn’t required to use. I only need output one.🤔
I installed intellij idea and ktor plugin (previously was android studio) and there was some websocket config so it seems to work with this setup, but only with generated client (not in browser)
3 Views