mirror of
https://github.com/SOR2171/Ktor_Server_Demo.git
synced 2026-10-24 02:51:10 +08:00
git init
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package io.github.sor2171
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.websocket.*
|
||||
import io.ktor.websocket.*
|
||||
import java.time.Duration
|
||||
|
||||
fun Application.configureRouting() {
|
||||
routing {
|
||||
get("/") {
|
||||
call.respondText("Hello, World!")
|
||||
}
|
||||
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"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.github.sor2171
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.websocket.*
|
||||
import io.ktor.websocket.*
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
fun Application.configureWebsockets() {
|
||||
install(WebSockets) {
|
||||
pingPeriod = 15.seconds
|
||||
timeout = 15.seconds
|
||||
maxFrameSize = Long.MAX_VALUE
|
||||
masking = false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.github.sor2171
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
io.ktor.server.cio.EngineMain.main(args)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
ktor:
|
||||
deployment:
|
||||
port: 8080
|
||||
application:
|
||||
modules:
|
||||
- io.github.sor2171.WebsocketsKt.configureWebsockets
|
||||
- io.github.sor2171.RoutingKt.configureRouting
|
||||
@@ -0,0 +1,12 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
<logger name="org.eclipse.jetty" level="INFO"/>
|
||||
<logger name="io.netty" level="INFO"/>
|
||||
</configuration>
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.github.sor2171
|
||||
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.testing.testApplication
|
||||
import kotlin.test.*
|
||||
|
||||
class ServerTest {
|
||||
|
||||
@Test
|
||||
fun `test root endpoint`() = testApplication {
|
||||
// loads default configuration
|
||||
configure()
|
||||
// verify server root returns 200
|
||||
assertEquals(HttpStatusCode.OK, client.get("/").status)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user