一款基于CheeseAPI的升级款Websocket插件。
Project description
CheeseAPI_Websocket
介绍
一款基于CheeseAPI的升级款Websocket插件,它能够解决在多worker下websocket的通讯问题,前提是需要引入redis。
安装
目前仅保证支持3.11及以上的python。
pip install CheeseAPI_Websocket
使用
CheeseAPI_Websocket是CheeseAPI的一款插件,它需要依赖于CheeseAPI才能运行。
import threading, time
from CheeseAPI import app, WebsocketClient, Response
from CheeseAPI_Websocket import websocket
app.modules = [ '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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cheeseapi_websocket-0.1.11.tar.gz
.
File metadata
- Download URL: cheeseapi_websocket-0.1.11.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fc841a92bba618aa519369e9c83fd5c465d9c5f7b107b9f6a4d323c2d4df971 |
|
MD5 | f0a9d92fabbd9b192e60b75ab0b9ff31 |
|
BLAKE2b-256 | 54efc9b8f1808e9cf2aacf9b3d65ab81d7dd5b3d86f34a3778cd6bdfad23dde6 |
File details
Details for the file cheeseapi_websocket-0.1.11-py2.py3-none-any.whl
.
File metadata
- Download URL: cheeseapi_websocket-0.1.11-py2.py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7814452a74e06668f58ae5e39b5531fbc44af8ed0e20e78408f98c228114449 |
|
MD5 | 8c97234861137cd7de97c5a01d12ff27 |
|
BLAKE2b-256 | 1979eba1a0b29150a3dbb7c4382c2a83318635472576881b1d9e3a92f4452d25 |