feat: 添加多个功能模块和工具脚本
- 新增websocket客户端和服务端实现 - 添加图片压缩工具和快速压缩脚本 - 实现学生信息处理相关API - 添加MQTT客户端和消息处理功能 - 更新.gitignore忽略更多文件类型 - 添加数据库操作工具和示例 - 实现多个测试脚本和工具类
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8776a57b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "base",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.12.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
import argparse
|
||||
import time
|
||||
|
||||
async def receive_messages(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"Listening on stream: {stream_id}")
|
||||
while True:
|
||||
try:
|
||||
message = await websocket.recv()
|
||||
print(f"Received: {message}")
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
print("Connection closed")
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"Connection error: {e}")
|
||||
|
||||
print("Reconnecting in 3 seconds...")
|
||||
await asyncio.sleep(3)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description='WebSocket Receiver Client')
|
||||
parser.add_argument('stream_id', type=str, nargs='?', default=str(int(time.time())),
|
||||
help='Stream ID (timestamp) to connect to. Should match the sender.')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.stream_id is None:
|
||||
print("Warning: No stream_id provided. Using current timestamp, which might not match sender.")
|
||||
|
||||
try:
|
||||
asyncio.run(receive_messages(123456))
|
||||
except KeyboardInterrupt:
|
||||
print("\nStopped by user")
|
||||
@@ -0,0 +1,46 @@
|
||||
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")
|
||||
Reference in New Issue
Block a user