用Python和Websockets库构建一个高性能、低延迟的实时消息推送服务

基础的服务端代码:

python 复制代码
import asyncio
import websockets
import json
from datetime import datetime

class MessageServer:
    def __init__(self):
        self.clients = set()  # 存储所有连接的客户端
        
    async def register(self, websocket):
        """注册新客户端"""
        self.clients.add(websocket)
        print(f"新客户端连接 当前连接数: {len(self.clients)}")
        
    async def unregister(self, websocket):
        """注销客户端"""
        self.clients.remove(websocket)
        print(f"客户端断开 当前连接数: {len(self.clients)}")
        
    async def broadcast(self, message, sender_ws=None):
        """广播消息给所有客户端"""
        if self.clients:
            # 过滤掉发送者自己 避免回显
            targets = {client for client in self.clients if client != sender_ws}
            if targets:
                await asyncio.gather(
                    *[client.send(json.dumps(message)) for client in targets],
                    return_exceptions=True
                )
                
    async def handle_client(self, websocket, path):
        """处理客户端连接"""
        await self.register(websocket)
        try:
            async for message in websocket:
                data = json.loads(message)
                # 添加时间戳
                data['timestamp'] = datetime.now().isoformat()
                print(f"收到消息: {data}")
                # 广播给其他客户端
                await self.broadcast(data, websocket)
        except websockets.exceptions.ConnectionClosed:
            pass
        finally:
            await self.unregister(websocket)

# 启动服务器
server = MessageServer()
start_server = websockets.serve(server.handle_client, "localhost", 8765)

print("WebSocket服务器启动在 ws://localhost:8765")
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

客户端代码

python 复制代码
import asyncio
import websockets
import json
import threading

class MessageClient:
    def __init__(self, uri):
        self.uri = uri
        self.websocket = None
        
    async def connect(self):
        """连接服务器"""
        self.websocket = await websockets.connect(self.uri)
        print("连接成功!")
        
    async def send_message(self, message):
        """发送消息"""
        if self.websocket:
            data = {
                'type': 'message',
                'content': message,
                'user': 'user_123'  # 实际项目中应该是真实用户ID
            }
            await self.websocket.send(json.dumps(data))
            
    async def listen(self):
        """监听服务器消息"""
        try:
            async for message in self.websocket:
                data = json.loads(message)
                print(f"[{data.get('timestamp', '')}] {data.get('user', '未知')}: {data.get('content', '')}")
        except websockets.exceptions.ConnectionClosed:
            print("连接已断开")
            
    def start_input_loop(self):
        """处理用户输入"""
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        
        async def input_handler():
            while True:
                message = input()
                if message.lower() == 'quit':
                    break
                await self.send_message(message)
                
        loop.run_until_complete(input_handler())

# 使用示例
async def main():
    client = MessageClient("ws://localhost:8765")
    await client.connect()
    
    # 启动输入线程
    input_thread = threading.Thread(target=client.start_input_loop)
    input_thread.daemon = True
    input_thread.start()
    
    # 监听消息
    await client.listen()

if __name__ == "__main__":
    asyncio.run(main())

性能优化首先是连接池管理。当连接数达到几千甚至上万时 内存占用会很大,解决办法是设置最大连接数限制:

python 复制代码
async def handle_client(self, websocket, path):
    if len(self.clients) >= MAX_CONNECTIONS:
        await websocket.close(code=1013, reason="服务器连接数已满")
        return
    # 其他处理逻辑...

高并发时如果直接广播 会造成阻塞。我现在都用Redis做消息队列了。

python 复制代码
import redis
import asyncio

class RedisMessageServer(MessageServer):
    def __init__(self):
        super().__init__()
        self.redis_client = redis.Redis(host='localhost', port=6379, db=0)
        self.pubsub = self.redis_client.pubsub()
        
    async def start_redis_listener(self):
        """监听Redis消息"""
        self.pubsub.subscribe('chat_messages')
        for message in self.pubsub.listen():
            if message['type'] == 'message':
                data = json.loads(message['data'])
                await self.broadcast(data)

部署的话我推荐用nginx做反向代理

python 复制代码
upstream websocket {
    server 127.0.0.1:8765;
}

server {
    listen 80;
    location /ws {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_read_timeout 86400;
    }
}

proxy_read_timeout要设置长一点。不然连接会被nginx强制断开。

生产环境一定要加心跳检测。我用的是每30秒发一次ping:

python 复制代码
async def heartbeat(self, websocket):
    try:
        while True:
            await websocket.ping()
            await asyncio.sleep(30)
    except:
        await self.unregister(websocket)
相关推荐
丈剑走天涯5 小时前
基于 Python 3 语法的 PyCharm 练习示例,涵盖列表操作、类型判断及数据清洗等核心知识点
windows·python·pycharm
————A6 小时前
Agent 接收用户上传文件
人工智能·笔记·python·状态模式
可乐鸡翅yeah_6 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案
开发语言·javascript·python·django·ecmascript·m3u8·m3u8在线
可乐鸡翅yeah_6 小时前
第三方接口对接 M3U8 流媒体排坑实战,解决外部服务商流兼容难题
前端·javascript·python·django·html·m3u8·m3u8在线
金融小师妹6 小时前
多因子智能推演:黄金震荡回升,杰克逊霍尔“沃什首秀”政策如何重塑金价路径的AI预测框架
大数据·人工智能·python·线性回归
阿童木写作6 小时前
跨马翻译:AI批量图片翻译与视频字幕翻译工具推荐
人工智能·python·音视频
阿kun要赚马内6 小时前
MCP(Model Context Protocol,模型上下文协议)
人工智能·python
来了就未晚6 小时前
Python基础 学习代码存储与命名规范
开发语言·python·学习
落魄大学生之流水线上谋生计7 小时前
LangGraph 基本用法指南
java·windows·python·langchain
华研前沿标杆游学7 小时前
宇树科技对外开放参访?机器人企业参访体验与行业核心价值解析
python