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()
请注意下列情况:
-
若传输的数据量过大,请使用原生的send方法,以避免为redis带来过大的负担。
-
多个不同数据源的服务器连接到同一个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['*'] = '*')
关闭连接,支持广播。
Metadata
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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cheeseapi_websocket-1.1.1.tar.gz | 3.7 kB | Without provenance | Details for cheeseapi_websocket-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cheeseapi_websocket-1.1.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for cheeseapi_websocket-1.1.1-py2.py3-none-any.whl |
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
|
Security for CheeseAPI-Websocket 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.