一款基于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()
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
Close
Hashes for cheeseapi_websocket-0.1.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69b4e4e4863f993fb288859654359fd28b7ea321e03a680ed6b4a1dfc2855104 |
|
MD5 | a375406531cc18ced93282c3443aa093 |
|
BLAKE2b-256 | 7337e60b9abcd1c08d68d08fce54317c4cea59cb79cd9bc02804326542fea3d9 |
Close
Hashes for cheeseapi_websocket-0.1.10-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dde7284f9ac6f0f41ed019e025fb2dd9ad8074dca950099ae1f13e87dadc3d81 |
|
MD5 | 728ecf748123e9b8d14e9f144dbd3517 |
|
BLAKE2b-256 | 44e554838a46ce97ea5f6301b120dfc0ed33502afa031a86069080a48ec54c96 |