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.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

sanic_sse_py3-1.0.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sanic-sse-py3-1.0.2.tar.gz
  • Upload date:
  • Size: 7.9 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.2.tar.gz
Algorithm Hash digest
SHA256 18a4162e071190662b0db6002c0650901033d5947d946bd18c870e52d7a42a73
MD5 211288a3444c597aed83c0f50be60bc6
BLAKE2b-256 ede4e8a03b7c85999697006353d6407b85fddc053fbd5e8bdb705a32566f3510

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sanic_sse_py3-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c979f94bdf958063fec450d493109d201dd84e9c6dbd764caf17a499255e9d0b
MD5 8743f7bcbaba927e6fa88f936521d6b9
BLAKE2b-256 109cf1c2140f93c065ff2b3387fe66128051a0ebe39ee6b69d87755b3551f36a

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