refactor: 优化前端网络层
- 优化ApiService,移除冗余代码 - 优化WebSocketClient连接处理
This commit is contained in:
@@ -98,21 +98,6 @@ class ApiService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun organizeConversation(conversationId: String): Result<Unit> {
|
|
||||||
return try {
|
|
||||||
val response = client.post("$BASE_URL/api/conversations/$conversationId/organize") {
|
|
||||||
contentType(ContentType.Application.Json)
|
|
||||||
}
|
|
||||||
if (response.status.isSuccess()) {
|
|
||||||
Result.success(Unit)
|
|
||||||
} else {
|
|
||||||
Result.failure(Exception("整理失败: ${response.status}"))
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Result.failure(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== 回忆录相关API ====================
|
// ==================== 回忆录相关API ====================
|
||||||
|
|
||||||
suspend fun getBookInfo(): Result<BookDto> {
|
suspend fun getBookInfo(): Result<BookDto> {
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ package com.huaga.life_echo.network
|
|||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.engine.okhttp.*
|
import io.ktor.client.engine.okhttp.*
|
||||||
import io.ktor.client.plugins.websocket.*
|
import io.ktor.client.plugins.websocket.*
|
||||||
import io.ktor.client.plugins.contentnegotiation.*
|
|
||||||
import io.ktor.client.plugins.logging.*
|
import io.ktor.client.plugins.logging.*
|
||||||
import io.ktor.serialization.kotlinx.json.*
|
|
||||||
import io.ktor.websocket.*
|
import io.ktor.websocket.*
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
@@ -20,12 +18,8 @@ import android.util.Log
|
|||||||
class WebSocketClient {
|
class WebSocketClient {
|
||||||
private val client = HttpClient(OkHttp) {
|
private val client = HttpClient(OkHttp) {
|
||||||
install(WebSockets)
|
install(WebSockets)
|
||||||
install(ContentNegotiation) {
|
// 注意:WebSocket不需要ContentNegotiation插件,因为消息是手动序列化的
|
||||||
json(Json {
|
// ContentNegotiation插件会导致尝试序列化WebSocket会话对象,引发序列化错误
|
||||||
ignoreUnknownKeys = true
|
|
||||||
encodeDefaults = false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
install(Logging) {
|
install(Logging) {
|
||||||
level = LogLevel.INFO
|
level = LogLevel.INFO
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user