add crud repo server and change a lot of different things
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.insanusmokrassar.postssystem.ktor.client
|
||||
|
||||
import com.insanusmokrassar.postssystem.ktor.asCorrectWebSocketUrl
|
||||
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
|
||||
import com.insanusmokrassar.postssystem.utils.common.safely
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.features.websocket.ws
|
||||
@@ -8,6 +9,7 @@ import io.ktor.http.cio.websocket.Frame
|
||||
import io.ktor.http.cio.websocket.readBytes
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
|
||||
/**
|
||||
* @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish
|
||||
@@ -63,3 +65,19 @@ inline fun <T> HttpClient.createStandardWebsocketFlow(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish
|
||||
* connection. Must return true in case if must be reconnected. By default always reconnecting
|
||||
*/
|
||||
inline fun <T> HttpClient.createStandardWebsocketFlow(
|
||||
url: String,
|
||||
crossinline checkReconnection: (Throwable?) -> Boolean = { true },
|
||||
deserializer: DeserializationStrategy<T>
|
||||
) = createStandardWebsocketFlow(
|
||||
url,
|
||||
checkReconnection
|
||||
) {
|
||||
standardKtorSerialFormat.decodeFromByteArray(deserializer, it)
|
||||
}
|
||||
|
||||
|
@@ -1,12 +1,15 @@
|
||||
package com.insanusmokrassar.postssystem.ktor.server
|
||||
|
||||
import com.insanusmokrassar.postssystem.ktor.CorrectCloseException
|
||||
import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat
|
||||
import com.insanusmokrassar.postssystem.utils.common.safely
|
||||
import io.ktor.http.cio.websocket.*
|
||||
import io.ktor.routing.Route
|
||||
import io.ktor.websocket.webSocket
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.SerializationStrategy
|
||||
|
||||
private suspend fun DefaultWebSocketSession.checkReceivedAndCloseIfExists() {
|
||||
if (incoming.poll() != null) {
|
||||
@@ -29,3 +32,14 @@ fun <T> Route.includeWebsocketHandling(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> Route.includeWebsocketHandling(
|
||||
suburl: String,
|
||||
flow: Flow<T>,
|
||||
serializer: SerializationStrategy<T>
|
||||
) = includeWebsocketHandling(
|
||||
suburl,
|
||||
flow
|
||||
) {
|
||||
standardKtorSerialFormat.encodeToByteArray(serializer, it)
|
||||
}
|
||||
|
@@ -30,6 +30,14 @@ suspend fun ApplicationCall.getParameterOrSendError(
|
||||
field: String
|
||||
) = parameters[field].also {
|
||||
if (it == null) {
|
||||
respond(HttpStatusCode.BadRequest, "request must contains $field")
|
||||
respond(HttpStatusCode.BadRequest, "Request must contains $field")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun ApplicationCall.getQueryParameterOrSendError(
|
||||
field: String
|
||||
) = request.queryParameters[field].also {
|
||||
if (it == null) {
|
||||
respond(HttpStatusCode.BadRequest, "Request query parametersmust contains $field")
|
||||
}
|
||||
}
|
||||
|
@@ -27,9 +27,7 @@ class WebsocketsTest {
|
||||
val server = createKtorServer(host = "127.0.0.1", port = port) {
|
||||
install(WebSockets)
|
||||
routing {
|
||||
includeWebsocketHandling(suburl, dataFlow) {
|
||||
standardKtorSerialFormat.encodeToByteArray(Int.serializer(), it)
|
||||
}
|
||||
includeWebsocketHandling(suburl, dataFlow, Int.serializer())
|
||||
}
|
||||
}.also {
|
||||
it.start(false)
|
||||
@@ -42,10 +40,9 @@ class WebsocketsTest {
|
||||
}
|
||||
val incomingWebsocketFlow = client.createStandardWebsocketFlow(
|
||||
"$serverUrl/$suburl",
|
||||
{ false } // always skip reconnection
|
||||
) {
|
||||
standardKtorSerialFormat.decodeFromByteArray(Int.serializer(), it)
|
||||
}
|
||||
{ false }, // always skip reconnection
|
||||
deserializer = Int.serializer()
|
||||
)
|
||||
|
||||
var currentlyCheckingData: Int? = null
|
||||
incomingWebsocketFlow.onEach {
|
||||
|
Reference in New Issue
Block a user