Merge pull request #163 from InsanusMokrassar/0.11.2

0.11.2
This commit is contained in:
InsanusMokrassar 2022-06-13 23:21:14 +06:00 committed by GitHub
commit c9c6d4c0c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 7 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 0.11.2
* `Ktor`:
* Support of `WebSockets` has been improved and added fixes inside of clients
## 0.11.1
* `Repos`

View File

@ -14,5 +14,5 @@ crypto_js_version=4.1.1
# Project data
group=dev.inmo
version=0.11.1
android_code_version=125
version=0.11.2
android_code_version=126

View File

@ -19,7 +19,7 @@ import kotlinx.serialization.DeserializationStrategy
* @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 <reified T> HttpClient.createStandardWebsocketFlow(
inline fun <reified T : Any> HttpClient.createStandardWebsocketFlow(
url: String,
noinline checkReconnection: suspend (Throwable?) -> Boolean = { true },
noinline requestBuilder: HttpRequestBuilder.() -> Unit = {}
@ -32,8 +32,8 @@ inline fun <reified T> HttpClient.createStandardWebsocketFlow(
do {
val reconnect = runCatchingSafely {
ws(correctedUrl, requestBuilder) {
for (received in incoming) {
sendSerialized(received.data)
while (isActive) {
send(receiveDeserialized<T>())
}
}
checkReconnection(null)

View File

@ -12,6 +12,7 @@ import io.ktor.websocket.send
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.SerializationStrategy
@Deprecated("This method will be removed soon")
fun <T> Route.includeWebsocketHandling(
suburl: String,
flow: Flow<T>,

View File

@ -15,7 +15,8 @@ import kotlinx.serialization.SerializationStrategy
inline fun <reified T : Any> Route.includeWebsocketHandling(
suburl: String,
flow: Flow<T>,
protocol: URLProtocol? = null
protocol: URLProtocol? = null,
noinline dataMapper: suspend WebSocketServerSession.(T) -> T? = { it }
) {
application.apply {
pluginOrNull(WebSockets) ?: install(WebSockets)
@ -23,7 +24,7 @@ inline fun <reified T : Any> Route.includeWebsocketHandling(
webSocket(suburl, protocol ?.name) {
safely {
flow.collect {
sendSerialized(it)
sendSerialized(dataMapper(it) ?: return@collect)
}
}
}