I am trying to use SSE to automatically update an image source imbedded in the html on a web app I've created. Although I've used Python and Flask to create several pages in my app, I'm new to SSE, and I'm trying to make work an SSE example based on .
The output does not appear as I would expect. Instead of getting an actual web page with the sentences appearing one by one, the browser shows the literal strings produced by the yield statement in the for-loop, complete with the substring "data:" and the double line feeds. I used 127.0.0.1/events as the URL. (See below.)
I placed the Python code in a file called "SSE_tester.py" (see below). Here is the code from that web page:
from flask import Flask, Response
app = Flask(__name__)
@app.route('/events')
def sse_handler():
def generate():
paragraph = [
"Hello, this is an example of a continuous text output.",
"It contains multiple sentences, each of which will be sent to the client as an event.",
"This is to simulate the functionality of Server-Sent Events (SSE).",
"We can use this method to push real-time updates.",
"End of sample text, thank you!",
]
for sentence in paragraph:
yield f"data: {sentence}\n\n"
import time
time.sleep(1)
return Response(generate(), mimetype='text/event-stream')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081, debug=True)
I don't see anything in the code above which tells the script where to find the browser API (except maybe app.route?). I am a total newbie when it comes to Javascript and I would not be surprised if there's some magic thing I'm supposed to do with JS that the author of this code assumes the reader knows and I don't. If that is the case, help on that would be much appreciated.
The example also contained code for the browser API, which I will repeat below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SSE Example
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744731766a4590506.html
评论列表(0条)