python asyncio websockets server

python websocket server在收到接受消息处理完后会默认关闭连接。需要在msg_handler里面加个while true就能一直保持连接了。

start_server = websockets.serve(msg_handler, "0.0.0.0", 29967)

asyncio.get_event_loop().run_until_complete(start_server)

asyncio.get_event_loop().run_forever()

async def msg_handler(websocket, path):

while True:

data = await websocket.recv()

//do someting

await websocket.send('hey')

相关推荐
ZoeLandia5 分钟前
TypeScript 04:类型断言与类型守卫
开发语言·前端·typescript
geovindu18 分钟前
rust: pool Pattern
开发语言·后端·设计模式·rust·对象池简写模式
坊钰22 分钟前
【LangGraph框架入门级】4. 支持联网搜索的智能代理系统
python·ai编程·langgraph
运维@小兵35 分钟前
安装Pycharm并破解
ide·python·pycharm
jimy141 分钟前
标准的动态类型定义与“多态”无关,非多态对象同样会出现动态类型不同于静态类型的情形
开发语言·c++
冬、不眠的夜41 分钟前
Nuitka 将 python 程序编译成 exe 指南
开发语言·python
CVer儿43 分钟前
起源1引擎相关知识点2--World Brush+func_detail
开发语言
范中勤1 小时前
Python Asyncio 协程与 LLM 异步并发原理技术文档(与 Java 对比|彻底澄清所有误区)
python·llm·协程·并发编程·asyncio
薛之谦_1 小时前
JAVA 多线程编程:从基础原理到实战应用
java·开发语言
Dmz丶1 小时前
前端转 AI 100 天 Day 21:FastAPI 入门——从命令行到 HTTP 服务
python