Skip to content

Commit e448c28

Browse files
committed
fix(sse-starlette): handle SSE events with data prefixed with ":ping" and fixed#97
The SSE event parser now correctly handles events where the data is prefixed with ":ping", which was previously ignored. This change ensures that all SSE events are properly processed, addressing an issue where certain events were being discarded.
1 parent 3188b9e commit e448c28

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/main/kotlin/cc/unitmesh/devti/llms/azure/ResponseBodyCallback.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class AutoDevHttpException(error: String, private val statusCode: Int) : Runtime
4646
* SSE.
4747
*/
4848
class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private val emitDone: Boolean) : Callback {
49+
val logger = logger<ResponseBodyCallback>()
50+
4951
override fun onResponse(call: Call, response: Response) {
5052
var reader: BufferedReader? = null
5153
try {
@@ -64,16 +66,7 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private va
6466
sse = when {
6567
line!!.startsWith("data:") -> {
6668
val data = line!!.substring(5).trim { it <= ' ' }
67-
// https://github.com/sysid/sse-starlette/issues/16
68-
// ```
69-
// event: ping
70-
// data: 2020-12-22 15:33:27.463789
71-
// ```
72-
if (data.startsWith(":ping") || data.startsWith(": ping")) {
73-
null
74-
} else {
75-
SSE(data)
76-
}
69+
SSE(data)
7770
}
7871

7972
line == "" && sse != null -> {
@@ -99,6 +92,11 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private va
9992
null
10093
}
10194

95+
// : ping
96+
line!!.startsWith(": ping") -> {
97+
null
98+
}
99+
102100
else -> {
103101
throw SSEFormatException("Invalid sse format! '$line'")
104102
}

0 commit comments

Comments
 (0)