client.py 2.68 KB
import json
import socket
import threading
import datetime
import asyncio
import time
import websockets

async def websocket_client():
    uri = "ws://127.0.0.1:7801/ws/1"
    # uri = "ws://192.168.3.191:7864/ws/1"
    # uri = "ws://127.0.0.1:7864/ws1/1"
    # async with websockets.connect(uri) as websocket:
    async with websockets.connect(uri=uri) as websocket:
        # 发送消息到服务器
        await websocket.send("Hello, Server!")
        # 从服务器接收消息
        while True:
            response = await websocket.recv()
            print(f"Received message from server: {response}")
            if response == "<rdfast stop>":
                break
        # 可以继续发送和接收消息
        # await websocket.send("Another message!")
        # another_response = await websocket.recv()
        # print(f"Received another message from server: {another_response}")

async def websocket_client_01():
    # uri = "ws://127.0.0.1:7801/ws/1"
    # uri = "ws://192.168.3.191:7864/ws/1"
    # uri = "ws://salesgpt.dev.glsx.com.cn/ws/1"
    uri = "wss://salesapi.glsx.com.cn/ws/1"
    # uri = "ws://192.168.5.30:7864/ws/1"
    # uri = "ws://192.168.3.191:7864/ws1/1"
    # uri = "ws://127.0.0.1:7864/ws1/1"
    # uri = "ws://127.0.0.1:7864/ws/1"
    # async with websockets.connect(uri) as websocket:
    async with websockets.connect(uri=uri, timeout=120) as websocket:
        chatgpt_start_time = time.time()
        # 发送消息到服务器
        python_dict = {
            "type": "summarize",
            # "type": "sparring",
            # "type": "chat",
            # "question": "你好",
            "question": "你好,关于2011款宝马X5xDrive35i领先版有什么信息",
            "history": json.dumps(["你好::你也好", "你好::你也好"], ensure_ascii=False),
        }
        send_data=json.dumps(python_dict, ensure_ascii=False)
        print("send_data:", send_data)
        await websocket.send(send_data)
        # 从服务器接收消息
        while True:
            response = await websocket.recv()
            print(f"Received message from server: {response}")
            if response == "<rdfast stop>":
                break
        chatgpt_end_time = time.time()
        elapsed_time = chatgpt_end_time - chatgpt_start_time
        print(f"访问 SalesAI 时间为 {elapsed_time:.2f} 秒")
        # 可以继续发送和接收消息
        # await websocket.send("Another message!")
        # another_response = await websocket.recv()
        # print(f"Received another message from server: {another_response}")

# 运行客户端
# asyncio.get_event_loop().run_until_complete(websocket_client())
asyncio.get_event_loop().run_until_complete(websocket_client_01())