跳转到内容

WebSocket API 提供实时双向通信,用于接收 USV 的实时数据流。

ws://localhost:8081

使用 JWT Token 进行认证:

const token = 'your-jwt-token';
const ws = new WebSocket(`ws://localhost:8081?token=${token}`);

发送:

{
"action": "subscribe",
"device_id": "USV-2024-001",
"streams": ["telemetry", "sensors"]
}

响应:

{
"type": "subscription_ack",
"streams": ["telemetry", "sensors"]
}
{
"type": "telemetry",
"device_id": "USV-2024-001",
"timestamp": "2024-01-15T10:30:00Z",
"data": {
"latitude": 31.2304,
"longitude": 121.4737,
"speed": 5.2,
"heading": 90,
"battery": 78
}
}
{
"type": "sensor",
"device_id": "USV-2024-001",
"sensor_type": "water_quality",
"timestamp": "2024-01-15T10:30:00Z",
"data": {
"ph": 7.2,
"dissolved_oxygen": 8.5,
"temperature": 25.3
}
}
const ws = new WebSocket('ws://localhost:8081?token=your-token');
ws.onopen = () => {
console.log('Connected to WebSocket');
// 订阅数据流
ws.send(JSON.stringify({
action: 'subscribe',
device_id: 'USV-2024-001',
streams: ['telemetry', 'sensors']
}));
};
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
switch(message.type) {
case 'telemetry':
updateMap(message.data);
break;
case 'sensor':
updateSensorDisplay(message.data);
break;
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
ws.onclose = () => {
console.log('WebSocket closed');
};
import asyncio
import websockets
import json
async def stream_data():
uri = "ws://localhost:8081?token=your-token"
async with websockets.connect(uri) as websocket:
# 订阅数据流
await websocket.send(json.dumps({
"action": "subscribe",
"device_id": "USV-2024-001",
"streams": ["telemetry", "sensors"]
}))
# 接收数据
async for message in websocket:
data = json.loads(message)
print(f"Received: {data}")
asyncio.run(stream_data())

查看更多请访问 API 概览

目录

页面