-
Notifications
You must be signed in to change notification settings - Fork 5
/
ServerTest.kt
42 lines (35 loc) · 1.29 KB
/
ServerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.ktorwsissue
import io.ktor.client.HttpClient
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.features.websocket.WebSockets
import io.ktor.client.features.websocket.webSocket
import io.ktor.client.request.get
import io.ktor.http.cio.websocket.send
import java.util.logging.Logger
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import org.junit.Test
class ServerTest {
private val ktorEngine: HttpClientEngine = OkHttp.create {
preconfigured = OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build()
}
private val serverIPAddress = "192.168.1.110"
private val logger: Logger = Logger.getLogger("Client")
@Test
fun testConnection() = runBlocking {
val ktorClient = HttpClient(ktorEngine) {
install(WebSockets)
}
val httpResponse = ktorClient.get<String>("http://${serverIPAddress}:13276/")
logger.info("Got HTTP response: $httpResponse")
ktorClient.webSocket("ws://${serverIPAddress}:13276/ws") {
logger.info("Connection established")
val incomingMessage = incoming.receive()
logger.info("Got message $incomingMessage")
send("bye")
}
}
}