结合了 okhttp 进行网络请求
val inputStream = response.body?.byteStream()
val reader = BufferedReader(InputStreamReader(inputStream))
var str: String?
while (true) {
str = reader.readLine()
if (str != null && str.isNotEmpty()) {
when{
str.startsWith("event:")->{
//处理数据
}
str.startsWith("data:")->{
val jsonStr = str.substring(5).trim()
if (jsonStr.equals("[DONE]")) {
//结束处理
break
}
//处理数据
}
}
}
}
和 Flow 结合
https://medium.com/@manoel.al.amaro/understand-kotlin-flow-coroutines-by-implementing-server-side-sent-sse-9e190ff5f24f