Skip to main content

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

Project description

CheeseAPI_Websocket

介绍

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

安装

目前仅保证支持3.11及以上的python。

pip install CheeseAPI_Websocket

对应CheeseAPI版本:

版本 CheeseAPI版本
1.0.3 1.3.*
1.0.2 1.2.*
1.0.1 1.1.*
1.0.0 1.0.*

使用

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

import threading, time

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

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

websocket.init() # 初始化redis连接

@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()

if __name__ == '__main__':
    app.run()

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

Websocket

from CheeseAPI_Websocket import websocket

websocket.init(host: IPv4 = app.server.host, port: Port = 6379, db: NonNegativeInt = 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.0.3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

cheeseapi_websocket-1.0.3-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: cheeseapi_websocket-1.0.3.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for cheeseapi_websocket-1.0.3.tar.gz
Algorithm Hash digest
SHA256 9126838f09bc4994d65f2e51826d8ae0bfe22ce553eaf8b6bc6148f5a26c4e87
MD5 295abb985ed2f1426e87f666b6083d30
BLAKE2b-256 87a810d72c072860489c48c98ed65a43fc2c37e27cde5ae952fc917e944473d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cheeseapi_websocket-1.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 002167d345ef5aa1a1901cd426ec27f1c74d42540efa850ce96a964e129efc15
MD5 95c8f538ad1f4e5d3ffd2700b2db8744
BLAKE2b-256 f023306a03dc8a495dbd6c0d8a19c8fffb2922b322219ca3350c72682c1daa0b

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