client.py
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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())