本文介紹普通HTTP互動方式與SSE模式的區別。在普通HTTP互動方式下,一次請求只會收到一個資料包作為結果,但請求時間會略微長一些。SSE模式下伺服器會推送多個事件,每產生一些資料後就會推送給用戶端。SSE模式更適用於即時資料更新的情境,可以實現動態產生文字的效果。
普通模式
在普通模式下,給服務端發送的每個請求都會收到一個資料包作為結果。
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Mon, 07 Aug 2023 09:44:02 GMT
Content-Type: application/json;charset=UTF-8
Connection: close
Vary: Accept-Encoding
x-oss-request-id: 64D0BCDD88339E32330E2D1D
x-oss-server-time: 5865
Content-Encoding: gzip
{
"RequestID": "64D0BCDD88339E32330E2D1D",
"Output": {
"Text": "A Ghost, the Communist Ghost, haunts the European continent. In order to holy suppression of this ghost, all forces of the old Europe, including the Pope and the Czar, Metternich and Guizot, the radical party in France and the police in Germany, have united. Is there any opposition party that is not denounced by its governing enemies as a Communist? And is there any opposition party that does not use the charge of Communism as a weapon against more progressive oppositionists and its reactionary enemies? From this fact we can draw two conclusions: Communism has been recognized by all the forces of Europe as a power; it is now the turn of the Communists to make themselves known to the world, to state their views, their aims, their intentions, and to publish their own manifestos in order to refute the myth about the Communist ghost. For this purpose, delegates of the Communist parties of all countries assembled in London and adopted the following manifesto, which is published in English, French, German, Italian, Flemish and Danish.",
"FinishReason": "stop"
}
}SSE模式
SSE模式,即Server-Sent Events的簡稱,是一種伺服器端到用戶端(瀏覽器)的單向事件推送。EventSource介面是HTML5規範的一部分。
SSE推送的訊息是UTF-8編碼的純文字。每次推送由若干個事件訊息組成,每個事件訊息之間用兩個換行分割(\n\n)。事件訊息中由若干行組成,每行以[key]: value\n的索引值對組成。其中key可選項為id、event和data。data中的內容是訊息本身。
在智能文檔服務的實現中,data中的內容為一個JSON結構體,結構內容為文檔中返回參數介紹。
在一次請求中,您收到的結果如下所示:
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Mon, 07 Aug 2023 09:44:36 GMT
Content-Type: text/event-stream;charset=UTF-8
Transfer-Encoding: chunked
Connection: close
x-oss-request-id: 64D0BD0388339E30331D2D1D
x-oss-server-time: 761
id: 0
event: message
data: {"RequestId":"","Output":{"Text":"A Ghost,","FinishReason":"null"},"Usage":{"InputTokens":320,"OutputTokens":3}}
id: 1
event: message
data: {"RequestId":"","Output":{"Text":"A Ghost, the Communist Ghost, haunts the European","FinishReason":"null"},"Usage":{"InputTokens":320,"OutputTokens":11}}
id: 2
event: message
data: {"RequestId":"","Output":{"Text":"A Ghost, the Communist Ghost, haunts the European continent. In order to holy suppression of","FinishReason":"null"},"Usage":{"InputTokens":320,"OutputTokens":19}}
......
id: 27
event: Result
data: {"RequestId":"","Output":{"Text":"A Ghost, the Communist Ghost, haunts the European continent. In order to holy suppression of this ghost, all forces of the old Europe, including the Pope and the Czar, Metternich and Guizot, the radical party in France and the police in Germany, have united. Is there any opposition party that is not denounced by its governing enemies as a Communist? And is there any opposition party that does not use the charge of Communism as a weapon against more progressive oppositionists and its reactionary enemies? From this fact we can draw two conclusions: Communism has been recognized by all the forces of Europe as a power; it is now the turn of the Communists to make themselves known to the world, to state their views, their aims, their intentions, and to publish their own manifestos in order to refute the myth about the Communist ghost. For this purpose, delegates of the Communist parties of all countries assembled in London and adopted the following manifesto, which is published in English, French, German, Italian, Flemish and Danish.","FinishReason":"stop"},"Usage":{"InputTokens":320,"OutputTokens":212}}
其中每次事件中的訊息內容包含增量的資料,也就是在請求中data內容逐步更新。