- 新增websocket客户端和服务端实现 - 添加图片压缩工具和快速压缩脚本 - 实现学生信息处理相关API - 添加MQTT客户端和消息处理功能 - 更新.gitignore忽略更多文件类型 - 添加数据库操作工具和示例 - 实现多个测试脚本和工具类
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import asyncio
|
|
import websockets
|
|
import json
|
|
import time
|
|
import argparse
|
|
from faker import Faker
|
|
|
|
fake = Faker("zh_CN")
|
|
|
|
|
|
async def send_messages(stream_id):
|
|
# Construct URL with the provided or generated stream_id
|
|
uri = f"ws://192.168.0.209:9999/ws/live/barrage/{stream_id}?access_token=59c74e75-32d5-4d15-ab99-fde861004502&TENANT-ID=1"
|
|
|
|
while True:
|
|
try:
|
|
print(f"Connecting to {uri}...")
|
|
async with websockets.connect(uri) as websocket:
|
|
print(f"Connected to stream: {stream_id}")
|
|
while True:
|
|
# Generate fake data
|
|
data = {
|
|
"content": fake.sentence(),
|
|
"author": fake.name(),
|
|
"type": "barrage",
|
|
}
|
|
message = json.dumps(data, ensure_ascii=False)
|
|
|
|
# Send message
|
|
await websocket.send(message)
|
|
print(f"Sent: {message}")
|
|
|
|
# Wait for 1 second
|
|
await asyncio.sleep(5)
|
|
except Exception as e:
|
|
print(f"Connection failed or ended: {e}")
|
|
|
|
print("Reconnecting in 3 seconds...")
|
|
await asyncio.sleep(3)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
asyncio.run(send_messages(123456))
|
|
except KeyboardInterrupt:
|
|
print("\nStopped by user")
|