Giter Site home page Giter Site logo

sse_chait's People

Contributors

rgcsh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

sse_chait's Issues

你好请问一个兼容问题

运行大佬您的项目作为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+'&nbsp;&nbsp;&nbsp;&nbsp;');
        }, false);
        source.addEventListener('error', function (event) {
            console.log('reconnected service!')
        }, false);
    })
</script>
</body>
</html>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.