Add realtime gateway and simulator bridge

This commit is contained in:
2026-03-27 21:06:17 +08:00
parent 0703fd47a2
commit 2c0fd4c549
36 changed files with 6852 additions and 1 deletions

149
realtime-gateway/README.md Normal file
View File

@@ -0,0 +1,149 @@
# Realtime Gateway
`realtime-gateway` 是一个独立于现有模拟器的 Go 实时设备数据网关工程。
当前目标:
- 以实时中转为核心
- 纯内存运行
- 单二进制部署
- 支持 WebSocket 接入
- 支持 channel 模型
- 支持内置管理台、实时窗口、流量统计
- 为后续规则、通知、归档、回放预留插件总线
## 文档入口
优先看这几份:
- 运行手册:[realtime-gateway-runbook.md](D:/dev/cmr-mini/doc/realtime-gateway-runbook.md)
- 架构方案:[realtime-device-gateway-architecture.md](D:/dev/cmr-mini/doc/realtime-device-gateway-architecture.md)
- 协议说明:[gateway-protocol-spec.md](D:/dev/cmr-mini/doc/gateway-protocol-spec.md)
- Tunnel 联调:[cloudflare-tunnel-dev-guide.md](D:/dev/cmr-mini/doc/cloudflare-tunnel-dev-guide.md)
- 老模拟器桥接说明:[README.md](D:/dev/cmr-mini/tools/mock-gps-sim/README.md)
## 快速开始
### 1. 启动网关
```powershell
cd D:\dev\cmr-mini\realtime-gateway
go run .\cmd\gateway -config .\config\tunnel-dev.json
```
### 2. 打开管理台
```text
http://127.0.0.1:18080/
```
### 3. 启动老模拟器
```powershell
cd D:\dev\cmr-mini
npm run mock-gps-sim
```
### 4. 打开模拟器页面
```text
http://127.0.0.1:17865/
```
### 5. 用老模拟器桥接新网关
在“新网关桥接”区域填写:
- 网关地址:`ws://127.0.0.1:18080/ws`
- `Producer Token / Channel Token`
- `Channel ID` 可选
- `Device ID`
## 目录结构
```text
realtime-gateway/
├── cmd/gateway
├── cmd/mock-consumer
├── cmd/mock-producer
├── config
├── deploy
├── internal/channel
├── internal/config
├── internal/gateway
├── internal/model
├── internal/plugin
├── internal/router
└── internal/session
```
## 构建
```powershell
cd D:\dev\cmr-mini\realtime-gateway
go build -o .\bin\gateway.exe .\cmd\gateway
go build -o .\bin\mock-producer.exe .\cmd\mock-producer
go build -o .\bin\mock-consumer.exe .\cmd\mock-consumer
```
或直接做一次完整编译检查:
```powershell
cd D:\dev\cmr-mini\realtime-gateway
go build ./...
```
## 默认地址
推荐开发配置:
- 网关 HTTP / Admin`http://127.0.0.1:18080/`
- 网关 WebSocket`ws://127.0.0.1:18080/ws`
- 模拟器页面:`http://127.0.0.1:17865/`
- 模拟器 mock WS`ws://127.0.0.1:17865/mock-gps`
## CLI 调试
### producer
```powershell
cd D:\dev\cmr-mini\realtime-gateway
go run .\cmd\mock-producer -device-id child-001 -topic telemetry.location -count 5
go run .\cmd\mock-producer -device-id child-001 -topic telemetry.heart_rate -bpm 148 -count 5
```
### consumer
```powershell
cd D:\dev\cmr-mini\realtime-gateway
go run .\cmd\mock-consumer -config .\config\consumer-gps-heart.example.json
```
### channel 模式
```powershell
cd D:\dev\cmr-mini\realtime-gateway
go run .\cmd\mock-producer -channel-id ch-xxxx -token <producer-token> -topic telemetry.location -count 5
go run .\cmd\mock-consumer -channel-id ch-xxxx -token <consumer-token> -topics telemetry.location,telemetry.heart_rate
```
## 当前已落地能力
- WebSocket 接入
- `authenticate`
- `join_channel`
- `subscribe`
- `publish`
- `snapshot`
- latest state 缓存
- channel 管理
- `cache_latest`
- `drop_if_no_consumer`
- 管理台
- 实时数据窗口
- GPS 轨迹预览
- 流量统计
更多运行和排障细节,直接看:
- [realtime-gateway-runbook.md](D:/dev/cmr-mini/doc/realtime-gateway-runbook.md)