Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python flask应用程序中广播服务器发送的事件?_Python_Html_Flask_Eventsource - Fatal编程技术网

如何在python flask应用程序中广播服务器发送的事件?

如何在python flask应用程序中广播服务器发送的事件?,python,html,flask,eventsource,Python,Html,Flask,Eventsource,我正在根据条件获取某些记录,并将它们存储在msgarr中 我在这里使用的是服务器发送的事件(我现在不希望有任何关于使用socketio或任何其他概念的建议)。当我打开浏览器时,来自msgarr的数据被完美地推送 但当我在另一台电脑或手机上打开另一个浏览器时,来自msgarr的数据会被推送到这个或另一个浏览器中。这是随机的 如何确保所有连接的浏览器都从msgarr获取数据 下面是我的python flask服务器代码: Python Flask服务器: @app.route('/_receive'

我正在根据条件获取某些记录,并将它们存储在msgarr中

我在这里使用的是服务器发送的事件(我现在不希望有任何关于使用socketio或任何其他概念的建议)。当我打开浏览器时,来自msgarr的数据被完美地推送

但当我在另一台电脑或手机上打开另一个浏览器时,来自msgarr的数据会被推送到这个或另一个浏览器中。这是随机的

如何确保所有连接的浏览器都从msgarr获取数据

下面是我的python flask服务器代码:

Python Flask服务器:

@app.route('/_receive')
def receive():

   msgarr=[]
   conn = sqlite3.connect(db_path)
   sql="SELECT * FROM MESSAGES WHERE ROU='U' and NAME!='%s'" % (session.get('username'))
   cursor=conn.execute(sql);
   for row in cursor:
       msg='<div style="float:left;padding:7px;margin:3px 0px 3px 8px;background-color:#FEFEFE;clear:both;max-width:290px;border-radius: 10px;border-bottom: 1px solid #BEBEBE;word-wrap: break-word">'+'<div style="color: #BBBBBB;    font-size: small;    font-family: serif;">'+row[0]+'</div>'+row[1]+'<div style="color: #BBBBBB;    font-size: small;    font-family: serif;">'+row[3]+'</div></div>'
       msgarr.append(msg)
       sql="UPDATE MESSAGES SET ROU='R' where TEXT='%s' and NAME!='%s'" % (row[1],session.get('username'))
       conn.execute(sql);
   conn.commit()
   conn.close()

   def eventStream():
       str1 = ''.join(msgarr)
       strlength = len(str1)
       if strlength > 0:
           yield "data: {}\n\n".format(str1)
    return Response(eventStream(), mimetype="text/event-stream")
var source = new EventSource('/_receive');
    source.onmessage=function(event) {

        $("#result").append(event.data);


    };