feat(network): 添加 WITS 数据接收功能并更新 WebSocket 地址
- 新增 recive_wits.py 文件用于通过 TCP 接收 WITS 数据- 实现自动重连机制和数据黏包/拆包处理- 更新 test_websocket.py 中的 WebSocket 连接地址- 修改端口号以适配新的服务配置main
parent
9c6f5c3b63
commit
ea2d12c74e
|
|
@ -0,0 +1,59 @@
|
|||
import socket
|
||||
import time
|
||||
|
||||
HOST = "192.168.1.41"
|
||||
PORT = 9928
|
||||
|
||||
def connect():
|
||||
"""建立 TCP 连接(带重试)"""
|
||||
while True:
|
||||
try:
|
||||
print(f"Connecting to {HOST}:{PORT} ...")
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((HOST, PORT))
|
||||
s.settimeout(5)
|
||||
print("Connected successfully!")
|
||||
return s
|
||||
except Exception as e:
|
||||
print(f"Connection failed: {e}, retrying in 3s...")
|
||||
time.sleep(3)
|
||||
|
||||
|
||||
def receive_wits_data(sock):
|
||||
"""持续接收 WITS 数据(自动处理黏包/拆包)"""
|
||||
buffer = ""
|
||||
|
||||
while True:
|
||||
try:
|
||||
data = sock.recv(4096)
|
||||
|
||||
# 服务器关闭
|
||||
if not data:
|
||||
print("Server closed connection.")
|
||||
return False
|
||||
|
||||
buffer += data.decode(errors="ignore")
|
||||
|
||||
# WITS 多为 \r\n 分隔
|
||||
while "\n" in buffer:
|
||||
line, buffer = buffer.split("\n", 1)
|
||||
line = line.strip()
|
||||
if line:
|
||||
print("Received:", line)
|
||||
|
||||
except socket.timeout:
|
||||
# 正常情况,继续接收即可
|
||||
continue
|
||||
except Exception as e:
|
||||
print("Error:", e)
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
while True:
|
||||
sock = connect()
|
||||
ok = receive_wits_data(sock)
|
||||
sock.close()
|
||||
|
||||
print("Reconnecting in 3 seconds...")
|
||||
time.sleep(3)
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import ssl
|
||||
import websocket
|
||||
|
||||
URL = "ws://192.168.1.41:9516/ws/" # 注意走 443,不要再连 8080 了
|
||||
URL = "ws://192.168.1.8:8084/ws" # 注意走 443,不要再连 8080 了
|
||||
# 如果你的 WS 路径是 /ws/,就写成上面这样;若是别的路径自己改
|
||||
|
||||
def on_message(ws, msg): print("收到:", msg)
|
||||
|
|
|
|||
Loading…
Reference in New Issue