rgcsh / sse_chait Goto Github PK
View Code? Open in Web Editor NEW通过flask_sse实现SSE(服务器发送事件)
通过flask_sse实现SSE(服务器发送事件)
运行大佬您的项目作为demo是完全没问题的,直接可用。
但是当融合到公司项目的时候发现对应的stream1的类型变成了text/html而不是eventsource,所以就进了路由下的根目录然后转发到其他页面,同时console里面出现报错
EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.
以下是我融合项目后的一部分代码
# -*- coding:utf-8 -*-
from flask import Blueprint, render_template, Flask, request, redirect, session, url_for, Response, send_from_directory
import redis
from flask_sse import sse
stream1 = Blueprint('stream1', __name__)
# 重定向到发送消息页面
@stream1.route('/')
def index():
return redirect(url_for('.index', _external=True) + 'tasks/' + 'channel_bob.html')
# 接收send_messages.html文件中接口发送的数据,并且通过sse实时推送给用户
@stream1.route('/messages', methods=['POST'])
def send_messages():
channel = request.values.get('channel')
message = request.values.get('message')
sse.publish({"message": message}, type='social', channel=channel)
#return jsonify({'code': 200, 'errmsg': 'success', 'data': None})
return "Message sent"
@stream1.route('/tasks/<path:path>')
def send_file(path):
return send_from_directory('templates/tasks/', path)
页面是
<!DOCTYPE html>
<html>
<head>
<title>Flask-SSE Quickstart</title>
</head>
<body>
<h1>Channel:channel_bob</h1>
<div id="get_message"></div>
<script src="/static/js/jquery-3.1.1.js" type="text/javascript" charset="utf-8"></script>
<script>
$(function () {
//只接收channel为channel_bob的消息
var source = new EventSource("/stream1?channel=channel_bob");
console.log(source);
source.addEventListener('social', function (event) {
var data = JSON.parse(event.data);
$('#get_message').append(data.message+' ');
}, false);
source.addEventListener('error', function (event) {
console.log('reconnected service!')
}, false);
})
</script>
</body>
</html>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.