-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sending empty data in sse in js client (#3176)
fix sending empty data in sse
- Loading branch information
Showing
4 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<h2>Server-Sent Events</h2> | ||
<div id="output"></div> | ||
<script> | ||
if (typeof EventSource !== "undefined") { | ||
var eventSource = new EventSource("http://localhost:8000/test_sse_empty"); | ||
eventSource.onmessage = function (event) { | ||
console.log(event.data); | ||
document.getElementById("output").innerHTML += event.data + "<br>"; | ||
}; | ||
eventSource.addEventListener("empty", (ev) => { | ||
console.log("empty arrived"); | ||
document.getElementById("output").innerHTML += "endx arrived" + "<br>"; | ||
eventSource.close(); | ||
}); | ||
|
||
eventSource.addEventListener("error", (err) => { | ||
console.error(err) | ||
document.getElementById("output").innerHTML += "error: " + err + "<br>"; | ||
}) | ||
} else { | ||
document.getElementById("output").innerHTML = | ||
// If browser does not support SSE | ||
"Sorry, the browser does not support SSE"; | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import AsyncIterator | ||
|
||
import uvicorn | ||
|
||
from litestar import Litestar, get | ||
from litestar.config.cors import CORSConfig | ||
from litestar.response import ServerSentEvent, ServerSentEventMessage | ||
from litestar.types import SSEData | ||
|
||
|
||
@get("/test_sse_empty") | ||
async def handler() -> ServerSentEvent: | ||
async def generate() -> AsyncIterator[SSEData]: | ||
event = ServerSentEventMessage(event="empty") | ||
yield event | ||
|
||
return ServerSentEvent(generate()) | ||
|
||
|
||
app = Litestar(route_handlers=[handler], cors_config=CORSConfig(allow_origins=["*"])) | ||
|
||
if __name__ == "__main__": | ||
uvicorn.run("sse_empty:app") |