AIGC标识 livekit api测试

import os
import sys
import asyncio
from livekit.api import LiveKitAPI, CreateRoomRequest, ListRoomsRequest, DeleteRoomRequest

async def run_tests():
    """执行远程 LiveKit API 测试"""
    # 从环境变量获取配置,支持远程服务器
    url = "http://xx.xx.xx.xx:7880"
    api_key = "devkey"
    api_secret = "secret"
    #‌API Key‌: devkey;API Secret‌: secret

    if not all([url, api_key, api_secret]):
        print("错误: 请设置 LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET 环境变量")
        sys.exit(1)

    print(f"正在连接远程服务器: {url}")
    
    # 初始化 API 客户端
    api = LiveKitAPI(
        url=url,
        api_key=api_key,
        api_secret=api_secret
    )

    try:
        # 1. 列出当前房间
        print("\n 获取房间列表...")
        list_req = ListRoomsRequest()
        rooms = await api.room.list_rooms(list_req)
        print(f"当前活跃房间数: {len(rooms.rooms)}")
        for room in rooms.rooms:
            print(f"  - {room.name} (参与者: {room.num_participants})")

        # 2. 创建测试房间
        test_room_name = f"remote-test-{os.getpid()}"
        print(f"\n 创建测试房间: {test_room_name}...")
        create_req = CreateRoomRequest(name=test_room_name)
        try:
            new_room = await api.room.create_room(create_req)
            print(f"房间创建成功: {new_room.name}")
        except Exception as e:
            print(f"创建房间失败: {e}")
            return

        # 3. 再次列出房间以验证
        print("\n 验证房间是否存在...")
        rooms = await api.room.list_rooms(ListRoomsRequest())
        found = any(r.name == test_room_name for r in rooms.rooms)
        print(f"房间验证结果: {'存在' if found else '不存在'}")

        # 4. 删除测试房间
        print(f"\n 清理测试房间: {test_room_name}...")
        delete_req = DeleteRoomRequest(room=test_room_name)
        try:
            await api.room.delete_room(delete_req)
            print("房间删除成功")
        except Exception as e:
            print(f"删除房间失败: {e}")

    except Exception as e:
        print(f"测试过程中发生错误: {e}")
    finally:
        await api.aclose()
        print("\n测试结束,连接已关闭")

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

 

posted @ 2026-09-04 18:44  LalaTina  阅读(7)  评论(0)    收藏  举报