Files
HikarinHeartRateMonitorService/README.md
2025-07-11 22:03:31 +08:00

79 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 心率监测 WebSocket 服务
这是一个基于ASP.NET Core的WebSocket服务用于接收心率监测设备的数据并转发给其他连接的客户端。
## 功能
- 接收心率数据(包含心率值、时间戳和设备名称)
- 验证客户端Token确保安全性
- 向其他所有已连接的客户端广播心率数据不包含Token
## 技术栈
- ASP.NET Core 9.0
- WebSockets
- System.Text.Json
## 数据格式
### 接收的数据格式
```json
{
"heartRate": 75,
"timestamp": "2023-04-10T15:30:45.123Z",
"deviceName": "HeartMonitor-X1",
"token": "1sZkzBKD3WpRT0eQ9Vk4"
}
```
### 广播的数据格式
```json
{
"heartRate": 75,
"timestamp": "2023-04-10T15:30:45.123Z",
"deviceName": "HeartMonitor-X1"
}
```
## 使用方式
1. 启动服务
2. 通过WebSocket连接到 `ws://localhost:5000/ws``wss://localhost:5001/ws`
3. 发送包含有效Token的心率数据JSON
4. 接收来自其他客户端的心率数据广播
## 客户端示例代码
```javascript
// 创建WebSocket连接
const socket = new WebSocket('ws://localhost:5000/ws');
// 连接建立时
socket.onopen = function(e) {
console.log('连接已建立');
// 发送心率数据
const heartRateData = {
heartRate: 75,
timestamp: new Date(),
deviceName: 'HeartMonitor-X1',
token: '1sZkzBKD3WpRT0eQ9Vk4'
};
socket.send(JSON.stringify(heartRateData));
};
// 接收消息
socket.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log('收到心率数据:', data);
};
// 连接关闭
socket.onclose = function(event) {
console.log('连接已关闭', event);
};
```