Skip to main content

aio sanic sse

Project description

sanic_sse ########

  1. 安装 ==========

.. code-block:: shell

pip install sanic-sse-py3

  1. 示例 ==========
  • 2.1 代码

.. code-block:: python

import os
import sys
from sanic import Sanic
from sanic.response import html

sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))

from sse.api.urls import sse_bgp
from sse import SseApp

app = Sanic(name="sse")

SSE_CONFIG = {
    "pubsub_options": {
        "redis_host": os.getenv("SSE_REDIS_HOST", "127.0.0.1"),
        "redis_port": int(os.getenv("SSE_REDIS_PORT", "16379")),
        "redis_passwd": os.getenv("SSE_REDIS_PASSWD", ""),
    },
    "ping_interval": int(os.getenv("SSE_PING_INTERVAL", 10)),

}


def init_app(_app):
    _app.ctx.sse_config = SSE_CONFIG
    SseApp(_app)
    _app.blueprint(sse_bgp)


@app.route("/index", methods=["GET"])
async def index(request):
    event = request.args.get("event", "test")
    url = f"sse/event/listen?event={event}"
    d = """
        <html>
        <body>
          <script>
                var source = new EventSource("%s");
                source.onmessage = function(e) {
                    console.log("xxxxxxx");
                    document.getElementById('response').innerHTML + e.data + "<br>";
                }
          </script>
          <h1>Getting server updates</h1>
          <div id="response"></div>
        </body>
    </html>
    """ % url
    return html(body=d)


if __name__ == "__main__":
    init_app(app)
    app.run(host="0.0.0.0", port=8008, workers=10)
  • 2.2 接口

.. code-block:: shell

GET /sse/event/send?event=test
GET /sse/event/listen?event=test&client_id=
GET /sse/event/terminate?event=test&client_id=

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanic-sse-py3-1.0.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

sanic_sse_py3-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file sanic-sse-py3-1.0.1.tar.gz.

File metadata

  • Download URL: sanic-sse-py3-1.0.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.7

File hashes

Hashes for sanic-sse-py3-1.0.1.tar.gz
Algorithm Hash digest
SHA256 eee956888a14bf8e4752a661720d79570ad7c8c1e8ce20c60249679ade9d8e90
MD5 0aa994b0f11bf1b80c373beda6384854
BLAKE2b-256 69114e2d551946e293bfd618cad535c3b3d37f34f7eb3662c39e32fc9593af56

See more details on using hashes here.

File details

Details for the file sanic_sse_py3-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sanic_sse_py3-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f24b04fe0640a04225dc54a82cafb8a2ce857b0850a64fddd57f055adcd2d667
MD5 bade8510a670eabd11b8579a02574df2
BLAKE2b-256 1f90b26459896dd2c81f43b2817e91649a3419cc983d7af98315b8945e18d6a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page