一款基于CheeseAPI的升级款Websocket插件
Project description
CheeseAPI_Websocket
介绍
一款基于CheeseAPI的升级款Websocket插件,它能够解决在多worker下websocket的通讯问题,前提是需要引入redis。
安装
目前仅支持linux python3.11,这里也推荐将python升级到3.11。
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()
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, sid: str | List[str] | Literal['*'] = '*')
发送消息,支持广播。
await websocket.async_send(path: str, message: str | bytes, 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
Close
Hashes for CheeseAPI_Websocket-0.1.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6862f64a925b9dc4374790b32fcaab38a302539b513f618073a6f0db06aa8f7 |
|
MD5 | be384e136f8afd54447bc27599445fdc |
|
BLAKE2b-256 | a28305a6b214195aa6222ec455d8e2809b22a940e35b28e8aff8e188be7fb7a1 |
Close
Hashes for CheeseAPI_Websocket-0.1.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8eabcea69d3f990fcf6fdeedd5d8981ccb5dd90091799480cba6b551dda91fb |
|
MD5 | 4ede6bff5f4e9d7f4208c046f7c752f3 |
|
BLAKE2b-256 | 0dc425c7aaf3fd33d7399f14da949af341f2b822bc068970cb5142c2d9a8a709 |