Skip to main content

一款基于CheeseAPI的升级款Websocket插件。

Project description

CheeseAPI_Websocket

介绍

一款基于CheeseAPI的升级款Websocket插件,它能够解决在多worker下websocket的通讯问题,前提是需要引入redis。

安装

系统要求:Unix,例如Linux、Mac等;不支持Windows,若有需要请使用Windows的WSL运行程序。

pip install CheeseAPI_Websocket

使用

CheeseAPI_Websocket是CheeseAPI的一款插件,它需要依赖于CheeseAPI才能运行。

默认情况下,它会连接到本地默认端口下的redis db0。

import threading, time

from CheeseAPI import app, WebsocketClient, Response
from CheeseAPI_Websocket import websocket

app.modules.append('CheeseAPI_Websocket') # 加入模块

@app.route.websocket('/')
class Test(WebsocketClient):
    ...

# 创建一个线程,在非协程环境下发送Websocket
@app.handle.server_afterStartingHandle
def test():
    def test0():
        while True:
            websocket.send('/', '你好')
            time.sleep(1)
threading.Thread(target = test, daemon = True).start()

# 在协程环境下发送Websocket
@app.route.post('/websocket')
async def test1():
    await websocket.async_send('/', '世界')
    return Response()

app.run()

请注意下列情况:

  1. 若传输的数据量过大,请使用原生的send方法,以避免为redis带来过大的负担。

  2. 多个不同数据源的服务器连接到同一个redis数据库,会导致websocket消息错乱。

Websocket

from CheeseAPI_Websocket import websocket

websocket.host: str = 'localhost'

连接的redis host。

websocket.port: int = 6379

连接的redis端口。

websocket.db: int = 0

连接的redis数据库。

websocket.send(path: str, message: str | bytes | dict | list, sid: str | List[str] | Literal['*'] = '*')

发送消息,支持广播。

await websocket.async_send(path: str, message: str | bytes | dict | list, sid: str | List[str] | Literal['*'] = '*')

发送消息,支持广播。

websocket.close(path: str, sid: str | List[str] | Literal['*'] = '*')

关闭连接,支持广播。

await websocket.async_close(path: str, sid: str | List[str] | Literal['*'] = '*')

关闭连接,支持广播。

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

cheeseapi_websocket-1.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

cheeseapi_websocket-1.1.0-py2.py3-none-any.whl (4.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cheeseapi_websocket-1.1.0.tar.gz.

File metadata

  • Download URL: cheeseapi_websocket-1.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for cheeseapi_websocket-1.1.0.tar.gz
Algorithm Hash digest
SHA256 61fb6a3d394ab7f47726df10d765cbed1503747bf919ab04c84b05efdae90db7
MD5 7d601c94584f80515db6a143b4eae83b
BLAKE2b-256 e2a9e06181174efa68e9395424a668905d4f5796e36777cb37455ae23a14a00e

See more details on using hashes here.

File details

Details for the file cheeseapi_websocket-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cheeseapi_websocket-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5c26d21b9d495c3457b50aecec002a14c85dd766083d6250e6116f4f6b6e271b
MD5 9414784101521da8eb757c7b2d498a5b
BLAKE2b-256 26183308066f7bb992451da5c9d4530317b1015f6d1fd01328020246c430820a

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