38 lines
1.2 KiB
Kotlin
38 lines
1.2 KiB
Kotlin
package eventDemo.plugins
|
|
|
|
import io.ktor.server.application.Application
|
|
import io.ktor.server.application.install
|
|
import io.ktor.server.routing.routing
|
|
import io.ktor.server.websocket.WebSockets
|
|
import io.ktor.server.websocket.pingPeriod
|
|
import io.ktor.server.websocket.timeout
|
|
import io.ktor.server.websocket.webSocket
|
|
import io.ktor.websocket.CloseReason
|
|
import io.ktor.websocket.Frame
|
|
import io.ktor.websocket.close
|
|
import io.ktor.websocket.readText
|
|
import java.time.Duration
|
|
|
|
fun Application.configureSockets() {
|
|
install(WebSockets) {
|
|
pingPeriod = Duration.ofSeconds(15)
|
|
timeout = Duration.ofSeconds(15)
|
|
maxFrameSize = Long.MAX_VALUE
|
|
masking = false
|
|
}
|
|
routing {
|
|
webSocket("/ws") {
|
|
// websocketSession
|
|
for (frame in incoming) {
|
|
if (frame is Frame.Text) {
|
|
val text = frame.readText()
|
|
outgoing.send(Frame.Text("YOU SAID: $text"))
|
|
if (text.equals("bye", ignoreCase = true)) {
|
|
close(CloseReason(CloseReason.Codes.NORMAL, "Client said BYE"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|