Skip to main content

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['*'] = '*')

关闭连接,支持广播。

Release files for CheeseAPI-Websocket 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CheeseAPI-Websocket 1.1.1
File Size Uploaded
cheeseapi_websocket-1.1.1.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for CheeseAPI-Websocket 1.1.1
File Interpreter ABI Platform
cheeseapi_websocket-1.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 8.5 kB

Release files / cheeseapi_websocket-1.1.1.tar.gz

Download URL cheeseapi_websocket-1.1.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2f0facee0b1cb88ebea8717996ffd4f781bcf3dc63c76faba9d84beed31d0f72
BLAKE2b-256 checksum
How to use checksums
2e30596ccfc9d98ad3d405f98febcbcda5e2c3a6b9948a15dff03bd60cee59a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release files / cheeseapi_websocket-1.1.1-py2.py3-none-any.whl

Download URL cheeseapi_websocket-1.1.1-py2.py3-none-any.whl
Size 4.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
375e08365e94313890ee9a437d42a8d3547833e3e64fc09b34b6cd59613af226
BLAKE2b-256 checksum
How to use checksums
499ca2274cfaf7a3587c943fb57ced22cb49a2cfef31bced8e96615b1be58223
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.11

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page