├── .gitignore
├── README.md
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── resources
├── application.conf
└── logback.xml
├── settings.gradle
├── src
├── Application.kt
├── SessionManager.kt
└── TestWsClient.kt
└── test
└── ApplicationTest.kt
/.gitignore:
--------------------------------------------------------------------------------
1 | /.gradle
2 | /.idea
3 | /out
4 | /build
5 | *.iml
6 | *.ipr
7 | *.iws
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | **This repo is part of WebRTC examples, see also:**
2 |
3 | - P2P Android client based on WebRTC - https://github.com/artem-bagritsevich/WebRTCAndroidAppExample
4 | - How to build WebRTC for Android - https://github.com/artem-bagritsevich/AndroidWebRTC
5 |
6 | Here is signaling server for p2p connection written in Kotlin with Ktor framework.
7 | **This is just example created in educational purpose, so don't use this code in production!**
8 |
9 | **How to run:**
10 | - To run the server, open the code in Intellij Idea and run main function inside Application class.
11 |
12 | **How to test:**
13 | - To run test client run main function inside the test client object.
14 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | jcenter()
4 | }
5 |
6 | dependencies {
7 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8 | }
9 | }
10 |
11 | apply plugin: 'kotlin'
12 | apply plugin: 'application'
13 |
14 | group 'com.webrtc.ktor.server'
15 | version '0.0.1'
16 | mainClassName = "io.ktor.server.netty.EngineMain"
17 |
18 | sourceSets {
19 | main.kotlin.srcDirs = main.java.srcDirs = ['src']
20 | test.kotlin.srcDirs = test.java.srcDirs = ['test']
21 | main.resources.srcDirs = ['resources']
22 | test.resources.srcDirs = ['testresources']
23 | }
24 |
25 | repositories {
26 | mavenLocal()
27 | jcenter()
28 | maven { url 'https://kotlin.bintray.com/ktor' }
29 | }
30 |
31 | dependencies {
32 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
33 | implementation "io.ktor:ktor-server-netty:$ktor_version"
34 | implementation "ch.qos.logback:logback-classic:$logback_version"
35 | implementation "io.ktor:ktor-client-core:$ktor_version"
36 | implementation "io.ktor:ktor-client-core-jvm:$ktor_version"
37 | implementation "io.ktor:ktor-client-cio:$ktor_version"
38 | implementation "io.ktor:ktor-server-core:$ktor_version"
39 | implementation "io.ktor:ktor-websockets:$ktor_version"
40 | implementation "io.ktor:ktor-client-websockets:$ktor_version"
41 | implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"
42 | testImplementation "io.ktor:ktor-server-tests:$ktor_version"
43 | }
44 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ktor_version=1.6.4
2 | kotlin.code.style=official
3 | kotlin_version=1.5.31
4 | logback_version=1.2.1
5 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/artem-bagritsevich/WebRTCKtorSignalingServerExample/1def9a498287c853e2230fcb621d58e9e24c4e68/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/resources/application.conf:
--------------------------------------------------------------------------------
1 | ktor {
2 | deployment {
3 | port = 8080
4 | port = ${?PORT}
5 | }
6 | application {
7 | modules = [ ApplicationKt.module ]
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = "webrtc_ktor_server"
2 |
--------------------------------------------------------------------------------
/src/Application.kt:
--------------------------------------------------------------------------------
1 | import io.ktor.application.*
2 | import io.ktor.http.cio.websocket.*
3 | import io.ktor.response.*
4 | import io.ktor.routing.*
5 | import io.ktor.websocket.*
6 | import kotlinx.coroutines.channels.ClosedReceiveChannelException
7 | import java.time.Duration
8 | import java.util.*
9 |
10 | fun main(args: Array): Unit = io.ktor.server.netty.EngineMain.main(args)
11 |
12 | @Suppress("unused") // Referenced in application.conf
13 | @kotlin.jvm.JvmOverloads
14 | fun Application.module(testing: Boolean = false) {
15 |
16 | install(WebSockets) {
17 | pingPeriod = Duration.ofSeconds(15)
18 | timeout = Duration.ofSeconds(15)
19 | maxFrameSize = Long.MAX_VALUE
20 | masking = false
21 | }
22 |
23 | routing {
24 |
25 | get("/") {
26 | call.respond("Hello from WebRTC signaling server")
27 | }
28 | webSocket("/rtc") {
29 | val sessionID = UUID.randomUUID()
30 | try {
31 | SessionManager.onSessionStarted(sessionID, this)
32 |
33 | for (frame in incoming) {
34 | when (frame) {
35 | is Frame.Text -> {
36 | SessionManager.onMessage(sessionID, frame.readText())
37 | }
38 | else -> Unit
39 | }
40 | }
41 | println("Exiting incoming loop, closing session: $sessionID")
42 | SessionManager.onSessionClose(sessionID)
43 | } catch (e: ClosedReceiveChannelException) {
44 | println("onClose $sessionID")
45 | SessionManager.onSessionClose(sessionID)
46 | } catch (e: Throwable) {
47 | println("onError $sessionID $e")
48 | SessionManager.onSessionClose(sessionID)
49 | }
50 | }
51 | }
52 | }
53 |
54 |
--------------------------------------------------------------------------------
/src/SessionManager.kt:
--------------------------------------------------------------------------------
1 | import io.ktor.http.cio.websocket.*
2 | import io.ktor.websocket.*
3 | import kotlinx.coroutines.*
4 | import kotlinx.coroutines.sync.Mutex
5 | import kotlinx.coroutines.sync.withLock
6 | import java.util.*
7 |
8 | object SessionManager {
9 |
10 | private val sessionManagerScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
11 | private val mutex = Mutex()
12 |
13 | private val clients = mutableMapOf()
14 |
15 | private var sessionState: WebRTCSessionState = WebRTCSessionState.Impossible
16 |
17 | fun onSessionStarted(sessionId: UUID, session: DefaultWebSocketServerSession) {
18 | sessionManagerScope.launch {
19 | mutex.withLock {
20 | if (clients.size > 1) {
21 | sessionManagerScope.launch(NonCancellable) {
22 | session.send(Frame.Close()) // only two peers are supported
23 | }
24 | return@launch
25 | }
26 | clients[sessionId] = session
27 | session.send("Added as a client: $sessionId")
28 | if (clients.size > 1) {
29 | sessionState = WebRTCSessionState.Ready
30 | }
31 | notifyAboutStateUpdate()
32 | }
33 | }
34 | }
35 |
36 | fun onMessage(sessionId: UUID, message: String) {
37 | when {
38 | message.startsWith(MessageType.STATE.toString(), true) -> handleState(sessionId)
39 | message.startsWith(MessageType.OFFER.toString(), true) -> handleOffer(sessionId, message)
40 | message.startsWith(MessageType.ANSWER.toString(), true) -> handleAnswer(sessionId, message)
41 | message.startsWith(MessageType.ICE.toString(), true) -> handleIce(sessionId, message)
42 | }
43 | }
44 |
45 | private fun handleState(sessionId: UUID) {
46 | sessionManagerScope.launch {
47 | clients[sessionId]?.send("${MessageType.STATE} $sessionState")
48 | }
49 | }
50 |
51 | private fun handleOffer(sessionId: UUID, message: String) {
52 | if (sessionState != WebRTCSessionState.Ready) {
53 | error("Session should be in Ready state to handle offer")
54 | }
55 | sessionState = WebRTCSessionState.Creating
56 | println("handling offer from $sessionId")
57 | notifyAboutStateUpdate()
58 | val clientToSendOffer = clients.filterKeys { it != sessionId }.values.first()
59 | clientToSendOffer.send(message)
60 | }
61 |
62 | private fun handleAnswer(sessionId: UUID, message: String) {
63 | if (sessionState != WebRTCSessionState.Creating) {
64 | error("Session should be in Creating state to handle answer")
65 | }
66 | println("handling answer from $sessionId")
67 | val clientToSendAnswer = clients.filterKeys { it != sessionId }.values.first()
68 | clientToSendAnswer.send(message)
69 | sessionState = WebRTCSessionState.Active
70 | notifyAboutStateUpdate()
71 | }
72 |
73 | private fun handleIce(sessionId: UUID, message: String) {
74 | println("handling ice from $sessionId")
75 | val clientToSendIce = clients.filterKeys { it != sessionId }.values.first()
76 | clientToSendIce.send(message)
77 | }
78 |
79 | fun onSessionClose(sessionId: UUID) {
80 | sessionManagerScope.launch {
81 | mutex.withLock {
82 | clients.remove(sessionId)
83 | sessionState = WebRTCSessionState.Impossible
84 | notifyAboutStateUpdate()
85 | }
86 | }
87 | }
88 |
89 | enum class WebRTCSessionState {
90 | Active, // Offer and Answer messages has been sent
91 | Creating, // Creating session, offer has been sent
92 | Ready, // Both clients available and ready to initiate session
93 | Impossible // We have less than two clients
94 | }
95 |
96 | enum class MessageType {
97 | STATE,
98 | OFFER,
99 | ANSWER,
100 | ICE
101 | }
102 |
103 | private fun notifyAboutStateUpdate() {
104 | clients.forEach { (_, client) ->
105 | client.send("${MessageType.STATE} $sessionState")
106 | }
107 | }
108 |
109 | private fun DefaultWebSocketServerSession.send(message: String) {
110 | sessionManagerScope.launch {
111 | this@send.send(Frame.Text(message))
112 | }
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/src/TestWsClient.kt:
--------------------------------------------------------------------------------
1 | import io.ktor.client.*
2 | import io.ktor.client.engine.cio.*
3 | import io.ktor.client.features.websocket.*
4 | import io.ktor.http.*
5 | import io.ktor.http.cio.websocket.*
6 | import kotlinx.coroutines.Dispatchers
7 | import kotlinx.coroutines.channels.consumeEach
8 | import kotlinx.coroutines.delay
9 | import kotlinx.coroutines.launch
10 | import kotlinx.coroutines.runBlocking
11 |
12 | /**
13 | * Used only for testing, send offer after 15 second delay
14 | */
15 | object TestWsClient1 {
16 | @JvmStatic
17 | fun main(args: Array) {
18 | runBlocking {
19 | val client = HttpClient(CIO).config { install(WebSockets) }
20 | client.ws(method = HttpMethod.Get, host = "127.0.0.1", port = 8080, path = "/rtc") {
21 | launch(Dispatchers.Default) {
22 | delay(15000L)
23 | send("${SessionManager.MessageType.OFFER} SDP asdaskfslkdfnlskdnfglksdnfklnsdkf")
24 | }
25 | incoming.consumeEach { frame ->
26 | when (frame) {
27 | is Frame.Text -> {
28 | println(frame.readText())
29 | }
30 | else -> Unit
31 | }
32 | }
33 | }
34 | }
35 | }
36 |
37 | /**
38 | * Used only for testing, subscribes for the offer and sends answer if offer received
39 | */
40 | object TestWsClient2 {
41 | @JvmStatic
42 | fun main(args: Array) {
43 | runBlocking {
44 | val client = HttpClient(CIO).config { install(WebSockets) }
45 | client.ws(method = HttpMethod.Get, host = "127.0.0.1", port = 8080, path = "/rtc") {
46 | incoming.consumeEach { frame ->
47 | when (frame) {
48 | is Frame.Text -> {
49 | val text = frame.readText()
50 | println(text)
51 | if (text.startsWith("offer", true)) {
52 | send("${SessionManager.MessageType.ANSWER} SDP saknfaslkdjflskdjfklnsdfasdasdsd")
53 | }
54 | }
55 | else -> Unit
56 | }
57 | }
58 | }
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/test/ApplicationTest.kt:
--------------------------------------------------------------------------------
1 | package com.webrtc.ktor.server
2 |
3 | import io.ktor.application.*
4 | import io.ktor.response.*
5 | import io.ktor.request.*
6 | import io.ktor.client.*
7 | import io.ktor.client.engine.cio.*
8 | import io.ktor.routing.*
9 | import io.ktor.http.*
10 | import io.ktor.websocket.*
11 | import io.ktor.http.cio.websocket.*
12 | import java.time.*
13 | import io.ktor.client.features.websocket.*
14 | import io.ktor.client.features.websocket.WebSockets
15 | import io.ktor.http.cio.websocket.Frame
16 | import kotlinx.coroutines.*
17 | import kotlinx.coroutines.channels.*
18 | import io.ktor.client.features.logging.*
19 | import kotlin.test.*
20 | import io.ktor.server.testing.*
21 | import module
22 |
23 | class ApplicationTest {
24 | @Test
25 | fun testRoot() {
26 | withTestApplication({ module(testing = true) }) {
27 | handleRequest(HttpMethod.Get, "/").apply {
28 | assertEquals(HttpStatusCode.OK, response.status())
29 | assertEquals("HELLO WORLD!", response.content)
30 | }
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------